Class: PinkShirt::SAX

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

Defined Under Namespace

Classes: Acronym, Base, Basic, BlockLevel, BoilerPlate, Images, Links, Lists, Preformatted, Script, Tables

Instance Method Summary collapse

Constructor Details

#initialize(fail_on_unknown = false) ⇒ SAX

Returns a new instance of SAX



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pink_shirt/sax.rb', line 17

def initialize(fail_on_unknown=false)
  @fail_on_unknown = fail_on_unknown
  @processors = [
    PinkShirt::SAX::Basic,
    PinkShirt::SAX::BoilerPlate,
    PinkShirt::SAX::Lists,
    PinkShirt::SAX::Links,
    PinkShirt::SAX::Images,
    PinkShirt::SAX::BlockLevel,
    PinkShirt::SAX::Preformatted,
    PinkShirt::SAX::Tables,
    PinkShirt::SAX::Acronym,
    PinkShirt::SAX::Script
  ]
  @running ||= {}
  @output = PinkShirt::Output.new
  @flags  = PinkShirt::Flags.new
end

Instance Method Details

#characters(string) ⇒ Object



64
65
66
# File 'lib/pink_shirt/sax.rb', line 64

def characters(string)
  plaintext(string)
end

#end_element(name) ⇒ Object



59
60
61
62
# File 'lib/pink_shirt/sax.rb', line 59

def end_element name
  processor = get_processor(name)
  get_processor(name).send("end_#{name}") if processor
end

#get_processor(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pink_shirt/sax.rb', line 40

def get_processor(name)
  klass = @processors.find{|processor|
    processor::TAGS.include? name
  }
  return @running[klass] if @running[klass]
  if klass
    @running[klass] = klass.new(@output, @flags)
  else
    raise StandardError, "unrecognised tag #{name}" if @fail_on_unknown
  end
  @running[klass]
end

#plaintext(string) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pink_shirt/sax.rb', line 68

def plaintext(string)
  if @flags.pre == true
    @output << string
  else
    @output << string.gsub(/[\t\n]/, "")
  end
end

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



53
54
55
56
57
# File 'lib/pink_shirt/sax.rb', line 53

def start_element name, attrs = []
  attrs = Hash[attrs]
  processor = get_processor(name)
  processor.send("start_#{name}", attrs) if processor
end

#to_textileObject



36
37
38
# File 'lib/pink_shirt/sax.rb', line 36

def to_textile
  @output.to_s
end