Text.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. class MarkedRenderer extends marked.Renderer
  2. image: (href, title, text) ->
  3. return ("<code>![#{text}](#{href})</code>")
  4. class Text
  5. toColor: (text, saturation=30, lightness=50) ->
  6. hash = 0
  7. for i in [0..text.length-1]
  8. hash += text.charCodeAt(i)*i
  9. hash = hash % 1777
  10. return "hsl(" + (hash % 360) + ",#{saturation}%,#{lightness}%)";
  11. renderMarked: (text, options={}) ->
  12. options["gfm"] = true
  13. options["breaks"] = true
  14. options["sanitize"] = true
  15. options["renderer"] = marked_renderer
  16. text = marked(text, options)
  17. return @fixHtmlLinks text
  18. emailLinks: (text) ->
  19. return text.replace(/([a-zA-Z0-9]+)@zeroid.bit/g, "<a href='?to=$1' onclick='return Page.message_create.show(\"$1\")'>$1@zeroid.bit</a>")
  20. # Convert zeronet html links to relaitve
  21. fixHtmlLinks: (text) ->
  22. if window.is_proxy
  23. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="http://zero')
  24. else
  25. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="')
  26. # Convert a single link to relative
  27. fixLink: (link) ->
  28. if window.is_proxy
  29. back = link.replace(/http:\/\/(127.0.0.1|localhost):43110/, 'http://zero')
  30. return back.replace(/http:\/\/zero\/([^\/]+\.bit)/, "http://$1") # Domain links
  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. getSiteUrl: (address) ->
  36. if window.is_proxy
  37. if "." in address # Domain
  38. return "http://"+address+"/"
  39. else
  40. return "http://zero/"+address+"/"
  41. else
  42. return "/"+address+"/"
  43. fixReply: (text) ->
  44. return text.replace(/(>.*\n)([^\n>])/gm, "$1\n$2")
  45. toBitcoinAddress: (text) ->
  46. return text.replace(/[^A-Za-z0-9]/g, "")
  47. jsonEncode: (obj) ->
  48. return unescape(encodeURIComponent(JSON.stringify(obj)))
  49. jsonDecode: (obj) ->
  50. return JSON.parse(decodeURIComponent(escape(obj)))
  51. fileEncode: (obj) ->
  52. if typeof(obj) == "string"
  53. return btoa(unescape(encodeURIComponent(obj)))
  54. else
  55. return btoa(unescape(encodeURIComponent(JSON.stringify(obj, undefined, '\t'))))
  56. utf8Encode: (s) ->
  57. return unescape(encodeURIComponent(s))
  58. utf8Decode: (s) ->
  59. return decodeURIComponent(escape(s))
  60. distance: (s1, s2) ->
  61. s1 = s1.toLocaleLowerCase()
  62. s2 = s2.toLocaleLowerCase()
  63. next_find_i = 0
  64. next_find = s2[0]
  65. match = true
  66. extra_parts = {}
  67. for char in s1
  68. if char != next_find
  69. if extra_parts[next_find_i]
  70. extra_parts[next_find_i] += char
  71. else
  72. extra_parts[next_find_i] = char
  73. else
  74. next_find_i++
  75. next_find = s2[next_find_i]
  76. if extra_parts[next_find_i]
  77. extra_parts[next_find_i] = "" # Extra chars on the end doesnt matter
  78. extra_parts = (val for key, val of extra_parts)
  79. if next_find_i >= s2.length
  80. return extra_parts.length + extra_parts.join("").length
  81. else
  82. return false
  83. parseQuery: (query) ->
  84. params = {}
  85. parts = query.split('&')
  86. for part in parts
  87. [key, val] = part.split("=")
  88. if val
  89. params[decodeURIComponent(key)] = decodeURIComponent(val)
  90. else
  91. params["url"] = decodeURIComponent(key)
  92. return params
  93. encodeQuery: (params) ->
  94. back = []
  95. if params.url
  96. back.push(params.url)
  97. for key, val of params
  98. if not val or key == "url"
  99. continue
  100. back.push("#{encodeURIComponent(key)}=#{encodeURIComponent(val)}")
  101. return back.join("&")
  102. highlight: (text, search) ->
  103. if not text
  104. return [""]
  105. parts = text.split(RegExp(search, "i"))
  106. back = []
  107. for part, i in parts
  108. back.push(part)
  109. if i < parts.length-1
  110. back.push(h("span.highlight", {key: i}, search))
  111. return back
  112. formatSize: (size) ->
  113. if isNaN(parseInt(size))
  114. return ""
  115. size_mb = size/1024/1024
  116. if size_mb >= 1000
  117. return (size_mb/1024).toFixed(1)+" GB"
  118. else if size_mb >= 100
  119. return size_mb.toFixed(0)+" MB"
  120. else if size/1024 >= 1000
  121. return size_mb.toFixed(2)+" MB"
  122. else
  123. return (parseInt(size)/1024).toFixed(2)+" KB"
  124. window.marked_renderer = new MarkedRenderer()
  125. window.is_proxy = (document.location.host == "zero" or window.location.pathname == "/")
  126. window.Text = new Text()