Class: CodelessCode::Formats::Term

Inherits:
Base
  • Object
show all
Defined in:
lib/codeless_code/formats/term.rb

Instance Attribute Summary

Attributes inherited from Base

#raw

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from CodelessCode::Formats::Base

Instance Method Details

#c(str) ⇒ Object



50
51
52
# File 'lib/codeless_code/formats/term.rb', line 50

def c(str)
  ColorizedString.new(str)
end

#callObject



23
24
25
26
27
# File 'lib/codeless_code/formats/term.rb', line 23

def call
  raw.split("\n\n")
     .map { |str| generate(to_xhtml(regex(str))) }
     .join("\n\n")
end

#generate(str) ⇒ Object



45
46
47
48
# File 'lib/codeless_code/formats/term.rb', line 45

def generate(str)
  return "" if str.length == 0
  MediaCloth::wiki_to_html(str, generator: TermGenerator.new(self))
end

#regex(str) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/codeless_code/formats/term.rb', line 34

def regex(str)
  [
    [/\/\/\w*$/, ''],
    [/^\|   .*/, c('\\0').green],
    [/<i>([^<]+)<\/i>/mi, "''\\1''"],
    [/<b>([^<]+)<\/b>/mi, "'''\\1'''"],
    [/<a[^>]+>([^<]+)<\/a>/mi, '[[\1]]'],
    [/\/(\w+)\//, "''\\1''"],
  ].inject(str) { |str, args| str = str.gsub(*args) }
end

#to_xhtml(str) ⇒ Object



29
30
31
32
# File 'lib/codeless_code/formats/term.rb', line 29

def to_xhtml(str)
  # MediaCloth expects XHTML-ish pages and chokes on ommited end tags
  Nokogiri::HTML(str).css('body > *').to_xhtml
end