Class: NwodkramParser

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/nwodkram_parser.rb

Constant Summary collapse

MARKDOWN =
{ 'p'          => [""                                                               , "\n"],
'a'          => ["["                                                              , lambda { "](#{@attr['href']})" }],
'img'        => [ lambda {"![#{@attr['title'] || @attr['alt']}](#{@attr['src']})"}, ""],
'li'         => [ lambda { @parent == "ul" ? "* " : "1. " }                       , "\n"],
'code'       => [""                                                               , ""],
'h1'         => ["# "                                                             , " #\n" ],
'h2'         => ["## "                                                            , " ##\n" ],
'h3'         => ["### "                                                           , " ###\n"],
'em'         => ["*"                                                              , "*"],
'blockquote' => ["> "                                                              , ""],
'strong'     => ["**"                                                             , "**"] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attrObject

Returns the value of attribute attr.



3
4
5
# File 'lib/nwodkram_parser.rb', line 3

def attr
  @attr
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/nwodkram_parser.rb', line 3

def name
  @name
end

Instance Method Details

#cdata_block(string) ⇒ Object



44
45
46
# File 'lib/nwodkram_parser.rb', line 44

def cdata_block(string)
  print string
end

#characters(string) ⇒ Object

content of the markup



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nwodkram_parser.rb', line 29

def characters(string)
  case @name
  when "code"
    string.split("\n").each {|line| 
      print "    #{line}\n" # 4 spaces to indicate code
    }
  when "img"
    print "" # image doesn't contain any children, normally
  when 'p','h1','h2','h3', 'li', 'ul','ol','blockquote'
    print string.chomp
  else
    print string
  end
end

#end_element(name) ⇒ Object



23
24
25
26
# File 'lib/nwodkram_parser.rb', line 23

def end_element(name)
  @name = name
  MARKDOWN[name] ? print(local_value(MARKDOWN[name][1])) : print("")
end

#start_element(name, attributes = []) ⇒ Object



17
18
19
20
21
# File 'lib/nwodkram_parser.rb', line 17

def start_element(name,attributes = [])
  @name, @attr = name, attr_hash(attributes)
  MARKDOWN[name] ? print(local_value(MARKDOWN[name][0])) : print("")
  track_parent
end