Text.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. class MarkedRenderer extends marked.Renderer
  2. image: (href, title, text) ->
  3. return ("<code>![#{text}](#{href})</code>")
  4. class Text
  5. toColor: (text) ->
  6. hash = 0
  7. for i in [0..text.length-1]
  8. hash += text.charCodeAt(i)*i
  9. hash = hash % 1000
  10. return "hsl(" + (hash % 360) + ",30%,50%)";
  11. renderMarked: (text, options={}) ->
  12. options["gfm"] = true
  13. options["breaks"] = true
  14. options["renderer"] = marked_renderer
  15. text = @fixReply(text)
  16. text = marked(text, options)
  17. text = @emailLinks text
  18. return @fixHtmlLinks text
  19. emailLinks: (text) ->
  20. return text.replace(/([a-zA-Z0-9]+)@zeroid.bit/, "<a href='?to=$1' onclick='return Page.message_create.show(\"$1\")'>$1@zeroid.bit</a>")
  21. # Convert zeronet html links to relaitve
  22. fixHtmlLinks: (text) ->
  23. if window.is_proxy
  24. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="http://zero')
  25. else
  26. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="')
  27. # Convert a single link to relative
  28. fixLink: (link) ->
  29. if window.is_proxy
  30. return link.replace(/http:\/\/(127.0.0.1|localhost):43110/, 'http://zero')
  31. else
  32. return link.replace(/http:\/\/(127.0.0.1|localhost):43110/, '')
  33. toUrl: (text) ->
  34. return text.replace(/[^A-Za-z0-9]/g, "+").replace(/[+]+/g, "+").replace(/[+]+$/, "")
  35. fixReply: (text) ->
  36. return text.replace(/(>.*\n)([^\n>])/gm, "$1\n$2")
  37. toBitcoinAddress: (text) ->
  38. return text.replace(/[^A-Za-z0-9]/g, "")
  39. jsonEncode: (obj) ->
  40. return unescape(encodeURIComponent(JSON.stringify(obj)))
  41. jsonDecode: (obj) ->
  42. return JSON.parse(decodeURIComponent(escape(obj)))
  43. fileEncode: (obj) ->
  44. if typeof(obj) == "string"
  45. return btoa(unescape(encodeURIComponent(obj)))
  46. else
  47. return btoa(unescape(encodeURIComponent(JSON.stringify(obj, undefined, '\t'))))
  48. utf8Encode: (s) ->
  49. return unescape(encodeURIComponent(s))
  50. utf8Decode: (s) ->
  51. return decodeURIComponent(escape(s))
  52. distance: (s1, s2) ->
  53. s1 = s1.toLocaleLowerCase()
  54. s2 = s2.toLocaleLowerCase()
  55. next_find_i = 0
  56. next_find = s2[0]
  57. match = true
  58. extra_parts = {}
  59. for char in s1
  60. if char != next_find
  61. if extra_parts[next_find_i]
  62. extra_parts[next_find_i] += char
  63. else
  64. extra_parts[next_find_i] = char
  65. else
  66. next_find_i++
  67. next_find = s2[next_find_i]
  68. if extra_parts[next_find_i]
  69. extra_parts[next_find_i] = "" # Extra chars on the end doesnt matter
  70. extra_parts = (val for key, val of extra_parts)
  71. if next_find_i >= s2.length
  72. return extra_parts.length + extra_parts.join("").length
  73. else
  74. return false
  75. parseQuery: (query) ->
  76. params = {}
  77. parts = query.split('&')
  78. for part in parts
  79. [key, val] = part.split("=")
  80. if val
  81. params[decodeURIComponent(key)] = decodeURIComponent(val)
  82. else
  83. params["url"] = decodeURIComponent(key)
  84. return params
  85. encodeQuery: (params) ->
  86. back = []
  87. if params.url
  88. back.push(params.url)
  89. for key, val of params
  90. if not val or key == "url"
  91. continue
  92. back.push("#{encodeURIComponent(key)}=#{encodeURIComponent(val)}")
  93. return back.join("&")
  94. window.is_proxy = (window.location.pathname == "/")
  95. window.marked_renderer = new MarkedRenderer()
  96. window.Text = new Text()