Class: Kramdown::Parser::Sekd

Inherits:
Kramdown
  • Object
show all
Defined in:
lib/zine_brewer/kramdown/parser/sekd.rb

Constant Summary collapse

LIST_START_OL =
/^(#{OPT_SPACE}(?:[^\\]|)\d+\.)(#{PATTERN_TAIL})/
FENCED_CODEBLOCK_MATCH =
/^(([~`]){3,})\s*?(\w[\d:\w-]*)?\s*?\n(.*?)^\1\2*\s*?\n/m
LANG_BY_EXT =
{
  "bash"=>"bsh", "clojure"=>"clj", "csharp"=>"cs", "c#"=>"cs", "el"=>"lisp",
  "erl"=>"erlang", "golang"=>"go", "haskell"=>"hs", "javascript"=>"js", "obj-c"=>"m",
  "objective-c"=>"m", "objc"=>"m", "pas"=>"pascal", "python"=>"py", "rlang"=>"r",
  "ruby"=>"rb", "sc"=>"scala", "visualbasic"=>"vb"
}.merge(Hash[*%w!
  apollo basic bsh c cc cpp clj cs csh css cyc cv dart erlang go hs htm html java js
  llvm m matlab ml mumps mxml lisp lua n pascal perl pl pm proto py r rb rd scala sh
  sql tcl tex vb vhdl wiki xhtml xml xq xsl yaml yml
!.map{|i| [i, i]}.flatten])
DIV_MATCH =
/^={3,}\s*?div\s*?\n(.*?)^={2,}\/div\s*?\n/mi
WRAPAROUND_MATCH =
/^={3,}\s*?wraparound\s*?\n(.*?)^={2,}\/wraparound\s*?\n/mi
COLUMN_MATCH =
/^={3,}\s*?column\s*?\n(.*?)^={2,}\/column\s*?\n/mi
DEFINITION_TABLE_MATCH =
/^={3,}\s*?dtable\s*?\n\n*(.*?)^={2,}\/dtable\s*?\n/mi
PAGE_MATCH =
/^<%-*\s*page\s*-*>/
FOOTNOTE_DEFINITION_START =
/^#{OPT_SPACE}\[\^(#{ALD_ID_NAME})\]:\s*?(.*?\n#{CODEBLOCK_MATCH})/
FOOTNOTE_MARKER_START =
/\[\^(#{ALD_ID_NAME})\]/

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Sekd

Returns a new instance of Sekd.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 16

def initialize(source, options)
  super

  parsers_replacing = {
    :@block_parsers => {:codeblock_fenced => :codeblock_fenced_sekd,
                        :footnote_definition => :footnote_definition_sekd},
    :@span_parsers  => {:footnote_marker => :footnote_marker_sekd}
  }
  parsers_replacing.each do |target, definitions|
    target_instance = instance_variable_get(target)
      definitions.each do |current, replacement|
      target_instance[target_instance.index(current)] = replacement
    end
  end

  @block_parsers.insert(5, :column, :definition_table, :wraparound,:div, :page)

  @page = 0
  @fn_counter = 0
  @fn_number = Hash.new{|h, k| h[k] = (@fn_counter += 1).to_s }
end

Instance Method Details

#parse_codeblock_fenced_sekdObject

Parse the fenced codeblock at the current location code highlighting by Google Prettify.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 42

def parse_codeblock_fenced_sekd
  if @src.check(self.class::FENCED_CODEBLOCK_MATCH)
    start_line_number = @src.current_line_number
    @src.pos += @src.matched_size
    el = new_block_el(:codeblock, @src[4].chomp, nil, :location => start_line_number)
    lang, linenums = @src[3].to_s.strip.split(/:/)
    lang_ext = LANG_BY_EXT[lang] || lang
    el.attr['class'] = "prettyprint" + (lang_ext.nil? ? ' nocode' : " lang-#{lang_ext}")
    el.attr['class'] += " linenums:#{linenums}" unless linenums.nil?
    @tree.children << el
    true
  else
    false
  end
end

#parse_columnObject



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 105

def parse_column
  if @src.check(self.class::COLUMN_MATCH)
    start_line_number = @src.current_line_number
    @src.pos += @src.matched_size
    el = Element.new(:column, nil, {'class' => 'columnSection'}, :location => start_line_number)
    parse_blocks(el, @src[1])
    update_attr_with_ial(el.attr, @block_ial) unless @block_ial.nil?
    @tree.children << el
    true
  else
    false
  end
end

#parse_definition_tableObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 122

def parse_definition_table
  if @src.check(self.class::DEFINITION_TABLE_MATCH)
    start_line_number = @src.current_line_number
    @src.pos += @src.matched_size
    el = Element.new(:definition_table, nil, nil, :location => start_line_number)
    parse_blocks(el, @src[1])
    update_attr_with_ial(el.attr, @block_ial) unless @block_ial.nil?
    @tree.children << el
    true
  else
    false
  end
end

#parse_divObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 71

def parse_div
  if @src.check(self.class::DIV_MATCH)
    start_line_number = @src.current_line_number
    @src.pos += @src.matched_size
    el = Element.new(:div, nil, nil, :location => start_line_number)
    parse_blocks(el, @src[1])
    update_attr_with_ial(el.attr, @block_ial) unless @block_ial.nil?
    @tree.children << el
    true
  else
    false
  end
end

#parse_footnote_definition_sekdObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 155

def parse_footnote_definition_sekd
  start_line_number = @src.current_line_number
  @src.pos += @src.matched_size
  if @fn_number.has_key?(@src[1])
    warning("Duplicate footnote name '#{@src[1]}' on line #{start_line_number} - overwriting")
  end
  a = %!<a href="#fnref:#{@fn_number[@src[1]]}">\\[#{@fn_number[@src[1]]}\\]</a>: #{@src[2].strip}!
  p = new_block_el(:p, nil, {'id' => "fn:#{@fn_number[@src[1]]}"})
  p.children << new_block_el(:raw_text, a, nil)
  if @tree.children.last.type == :footnote_definition_sekd
    @tree.children.last.children << p
  elsif [-1, -2].map{|i| @tree.children[i].type } == [:blank, :footnote_definition_sekd]
    @tree.children.pop
    @tree.children.last.children << p
  else
    f = new_block_el(:footnote_definition_sekd, nil, {'class' => 'columnSection footnotes'},
                     :location => start_line_number)
    f.children << p
    @tree.children << f
  end
  true
end

#parse_footnote_marker_sekdObject



181
182
183
184
185
186
187
188
189
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 181

def parse_footnote_marker_sekd
  start_line_number = @src.current_line_number
  @src.pos += @src.matched_size
  unless @fn_number.has_key?(@src[1])
    warning("No footnote marker '#{@src[1]}' on line #{start_line_number} - missing")
  end
  @tree.children << new_block_el(:footnote_marker_sekd, @fn_number[@src[1]], nil,
                                 :location => start_line_number)
end

#parse_pageObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 140

def parse_page
  if @src.check(self.class::PAGE_MATCH)
    start_line_number = @src.current_line_number
    @src.pos += @src.matched_size
    page = (@page > 0 ? "</div>\n<!-- page_delimiter -->\n" : '') + "<div id=\"p#{@page+=1}\">"
    @tree.children << new_block_el(:page, page, nil, :location => start_line_number)
    true
  else
    false
  end
end

#parse_wraparoundObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/zine_brewer/kramdown/parser/sekd.rb', line 88

def parse_wraparound
  if @src.check(self.class::WRAPAROUND_MATCH)
    start_line_number = @src.current_line_number
    @src.pos += @src.matched_size
    el = Element.new(:div, nil, {'class' => 'imgLRBlock cf'}, :location => start_line_number)
    parse_blocks(el, @src[1])
    update_attr_with_ial(el.attr, @block_ial) unless @block_ial.nil?
    @tree.children << el
    true
  else
    false
  end
end