Guest User

Untitled

a guest
Feb 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. require "html_filter"
  2.  
  3. class TextileFilter < HTMLFilter
  4. # Replace h[1-6]. Tjo with <h[1-6]>Tjo</h[1-6]>
  5. (1..6).each do |i|
  6. tag = "h#{i}"
  7.  
  8. define_rule(on_one_line(chars("#{tag}. ") + capture)) do |match|
  9. content_tag tag, match.captures.first
  10. end
  11. end
  12.  
  13. star = char("*")
  14.  
  15. # Replace *tjo* with <strong>tjo</strong>
  16. define_rule(star + capture + star) do |match|
  17. content_tag :strong, match.captures.first
  18. end
  19.  
  20. define_rule(/!(.*?)!/) do |match|
  21. tag :img, :src => match.captures.first
  22. end
  23.  
  24. at = char("@")
  25.  
  26. # Replace @tjo@ with <code>tjo</code>
  27. define_rule(at + capture + at) do |match|
  28. content_tag :code, match.captures.first
  29. end
  30.  
  31. define_rule(quoted(capture) + char(":") + capture + white_space) do |match|
  32. content_tag :a, match.captures.first, :href => match.captures.second
  33. end
  34.  
  35. # define_rule(/\n\n+/) { "\n\n" }
  36.  
  37. define_rule(on_one_line(capture + newline)) do |match|
  38. if enclosing_tag(match.captures.first)
  39. match
  40. else
  41. match.to_s + tag(:br) + "\n"
  42. end
  43. end
  44.  
  45. define_rule(/(\n\n|\A)(.*?)(\n\n|\Z)/) do |match|
  46. if is_block_level_tag? enclosing_tag(match)
  47. # Don't put block level tags in <p>s
  48. match
  49. else
  50. "\n\n" + content_tag(:p, match.captures.second) + "\n\n"
  51. end
  52. end
  53. end
Add Comment
Please, Sign In to add comment