Module: MaRuKu::In::Markdown::BlockLevelParser

Includes:
Helpers, SpanLevelParser, Strings
Included in:
MDDocument
Defined in:
lib/maruku.rb,
lib/maruku/input/parse_doc.rb,
lib/maruku/input/linesource.rb,
lib/maruku/input/parse_block.rb

Defined Under Namespace

Classes: BlockContext, LineSource

Constant Summary

Constants included from SpanLevelParser

SpanLevelParser::CharSource, SpanLevelParser::Close_class, SpanLevelParser::EscapedCharInInlineCode, SpanLevelParser::EscapedCharInQuotes, SpanLevelParser::EscapedCharInText, SpanLevelParser::IgnoreWikiLinks, SpanLevelParser::Punct_class, SpanLevelParser::Rules

Constants included from Strings

Strings::TAB_SIZE

Instance Method Summary collapse

Methods included from SpanLevelParser

#apply_one_rule!, #educate, #extension_meta, #interpret_extension, #md_al, #merge_ial, #parse_span, #read_attribute_list, #read_em, #read_email_el, #read_emstrong, #read_footnote_ref, #read_image, #read_inline_code, #read_inline_html, #read_link, #read_quoted, #read_quoted_or_unquoted, #read_ref_id, #read_simple, #read_span, #read_strong, #read_url, #read_url_el, #read_xml_instr_span

Methods included from Helpers

#md_abbr, #md_abbr_def, #md_ald, #md_br, #md_code, #md_codeblock, #md_el, #md_em, #md_email, #md_emstrong, #md_entity, #md_foot_ref, #md_footnote, #md_header, #md_hrule, #md_html, #md_ial, #md_im_image, #md_im_link, #md_image, #md_li, #md_link, #md_par, #md_quote, #md_ref_def, #md_strong, #md_url, #md_xml_instr

Methods included from Strings

#parse_email_headers, #sanitize_ref_id, #spaces_before_first_char, #split_lines, #strip_indent, #unquote

Instance Method Details

#eventually_comes_a_def_list(src) ⇒ Object

If current line is text, a definition list is coming if 1) text,empty,*,definition



573
574
575
# File 'lib/maruku/input/parse_block.rb', line 573

def eventually_comes_a_def_list(src)
  src.tell_me_the_future =~ %r{^t+e?d}x
end

#execute_code_blocksObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/maruku/input/parse_doc.rb', line 123

def execute_code_blocks
  each_element(:xml_instr) do |e|
    if e.target == 'maruku'
      result = safe_execute_code(e, e.code)
      if result.kind_of?(String)
        puts "Result is : #{result.inspect}"
      end
    end
  end
end

#expand_attribute_list(al, result) ⇒ Object

Expands an attribute list in an Hash



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/maruku/input/parse_doc.rb', line 72

def expand_attribute_list(al, result)
  al.each do |k, v|
    case k
    when :class
      if result[:class]
        result[:class] << " " << v
      else
        result[:class] = v
      end
    when :id
      result[:id] = v
    when :ref
      if self.ald[v]
        already = (result[:expanded_references] ||= [])
        if !already.include?(v)
          already << v
          expand_attribute_list(self.ald[v], result)
        else
          already << v
          maruku_error "Circular reference between labels.\n\n" +
          "Label #{v.inspect} calls itself via recursion.\nThe recursion is " +
            already.map(&:inspect).join(' => ')
        end
      else
        if result[:unresolved_references]
          result[:unresolved_references] << " " << v
        else
          result[:unresolved_references] = v
        end

        # $stderr.puts "Unresolved reference #{v.inspect} (avail: #{self.ald.keys.inspect})"
        result[v.to_sym] = true
      end
    else
      result[k.to_sym] = v
    end
  end
end

#parse_blocks(src) ⇒ Object

Input is a LineSource



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/maruku/input/parse_block.rb', line 26

def parse_blocks(src)
  output = BlockContext.new

  # run state machine
  while src.cur_line
    next if check_block_extensions(src, output, src.cur_line)

    md_type = src.cur_line.md_type

    # Prints detected type (useful for debugging)
    #puts "parse_blocks #{md_type}|#{src.cur_line}"
    case md_type
    when :empty
      output << :empty
      src.ignore_line
    when :ial
      m = InlineAttributeList.match src.shift_line
      content = m[1] || ""
      src2 = CharSource.new(content, src)
      interpret_extension(src2, output)
    when :ald
      output << read_ald(src)
    when :text
      # paragraph, or table, or definition list
      read_text_material(src, output)
    when :header2, :hrule
      # hrule
      src.shift_line
      output << md_hrule
    when :header3
      output << read_header3(src)
    when :ulist, :olist
      list_type = (md_type == :ulist) ? :ul : :ol
      li = read_list_item(src)
      # append to current list if we have one
      if output.last.kind_of?(MDElement) &&
          output.last.node_type == list_type then
        output.last.children << li
      else
        output << md_el(list_type, li)
      end
    when :quote
      output << read_quote(src)
    when :code
      e = read_code(src)
      output << e if e
    when :raw_html
      # More extra hacky stuff - if there's more than just HTML, we either wrap it
      # in a paragraph or break it up depending on whether it's an inline element or not
      e = read_raw_html(src)
      unless e.empty?
        if e.first.parsed_html &&
            (first_node_name = e.first.parsed_html.first_node_name) &&
            HTML_INLINE_ELEMS.include?(first_node_name) &&
            !%w(svg math).include?(first_node_name)
          content = [e.first]
          if e.size > 1
            content.concat(e[1].children)
          end
          output << md_par(content)
        else
          output.concat(e)
        end
      end
    when :footnote_text
      output << read_footnote_text(src)
    when :ref_definition
      if src.parent && src.cur_index == 0
        read_text_material(src, output)
      else
        read_ref_definition(src, output)
      end
    when :abbreviation
      output << read_abbreviation(src)
    when :xml_instr
      read_xml_instruction(src, output)
    else # warn if we forgot something
      line = src.cur_line
      maruku_error "Ignoring line '#{line}' type = #{md_type}", src
      src.shift_line
    end
  end

  merge_ial(output, src, output)
  output.delete_if {|x| x.kind_of?(MDElement) && x.node_type == :ial }

  # get rid of empty line markers
  output.delete_if {|x| x == :empty }

  # See for each list if we can omit the paragraphs
  # TODO: do this after
  output.each do |c|
    # Remove paragraphs that we can get rid of
    if [:ul, :ol].include?(c.node_type) && c.children.none?(&:want_my_paragraph)
      c.children.each do |d|
        if d.children.first && d.children.first.node_type == :paragraph
          d.children = d.children.first.children + d.children[1..-1]
        end
      end
    elsif c.node_type == :definition_list && c.children.none?(&:want_my_paragraph)
      c.children.each do |definition|
        definition.definitions.each do |dd|
          if dd.children.first.node_type == :paragraph
            dd.children = dd.children.first.children + dd.children[1..-1]
          end
        end
      end
    end
  end

  output
end

#parse_doc(s) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maruku/input/parse_doc.rb', line 6

def parse_doc(s)
  # Remove BOM if it is present
  s = s.sub(/^\xEF\xBB\xBF/u, '')
  meta2 = parse_email_headers(s)
  data = meta2.delete :data

  self.attributes.merge! meta2

=begin maruku_doc
Attribute: encoding
Scope:     document
Summary:   Encoding for the document.

If the `encoding` attribute is specified, then the content
will be converted from the specified encoding to UTF-8.
=end

  enc = self.attributes.delete(:encoding) || 'utf-8'
  if enc.downcase != 'utf-8'
    # Switch to ruby 1.9 String#encode
    # with backward 1.8 compatibility
    if data.respond_to?(:encode!)
      data.encode!('UTF-8', enc)
    else
      require 'iconv'
      data = Iconv.new('utf-8', enc).iconv(data)
    end
  end

  @children = parse_text_as_markdown(data)

  if markdown_extra?
    self.search_abbreviations
    self.substitute_markdown_inside_raw_html
  end

  self.toc = create_toc

  # use title if not set
  self.attributes[:title] ||= toc.header_element.children.join if toc.header_element

  # Now do the attributes magic
  each_element do |e|
    # default attribute list
    if default = self.ald[e.node_type.to_s]
      expand_attribute_list(default, e.attributes)
    end
    expand_attribute_list(e.al, e.attributes)
#     puts "#{e.node_type}: #{e.attributes.inspect}"
  end

=begin maruku_doc
Attribute: unsafe_features
Scope:     global
Summary:   Enables execution of XML instructions.

Disabled by default because of security concerns.
=end

  if Maruku::Globals[:unsafe_features]
    self.execute_code_blocks
    # TODO: remove executed code blocks
  end
end

#parse_text_as_markdown(text) ⇒ Object

Splits the string and calls parse_lines_as_markdown



19
20
21
22
23
# File 'lib/maruku/input/parse_block.rb', line 19

def parse_text_as_markdown(text)
  lines =  split_lines(text)
  src = LineSource.new(lines)
  parse_blocks(src)
end

#read_abbreviation(src) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/maruku/input/parse_block.rb', line 327

def read_abbreviation(src)
  unless (l = src.shift_line) =~ Abbreviation
    maruku_error "Bug: it's Andrea's fault. Tell him.\n#{l.inspect}"
  end

  abbr = $1
  desc = $2

  if !abbr || abbr.empty?
    maruku_error "Bad abbrev. abbr=#{abbr.inspect} desc=#{desc.inspect}"
  end

  self.abbreviations[abbr] = desc

  md_abbr_def(abbr, desc)
end

#read_ald(src) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/maruku/input/parse_block.rb', line 159

def read_ald(src)
  if (l = src.shift_line) =~ AttributeDefinitionList
    id = $1
    al = read_attribute_list(CharSource.new($2, src))
    self.ald[id] = al;
    md_ald(id, al)
  else
    maruku_error "Bug Bug:\n#{l.inspect}"
    nil
  end
end

#read_code(src) ⇒ Object



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/maruku/input/parse_block.rb', line 456

def read_code(src)
  # collect all indented lines
  lines = []
  while src.cur_line && [:code, :empty].include?(src.cur_line.md_type)
    lines << strip_indent(src.shift_line, 4)
  end

  #while lines.last && (lines.last.md_type == :empty )
  while lines.last && lines.last.strip.size == 0
    lines.pop
  end

  while lines.first && lines.first.strip.size == 0
    lines.shift
  end

  return nil if lines.empty?

  source = lines.join("\n")

  md_codeblock(source)
end

#read_definition(src) ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/maruku/input/parse_block.rb', line 577

def read_definition(src)
  # Read one or more terms
  terms = []
  while src.cur_line && src.cur_line.md_type == :text
    terms << md_el(:definition_term, parse_span(src.shift_line))
  end

  want_my_paragraph = false

  raise "Chunky Bacon!" unless src.cur_line

  # one optional empty
  if src.cur_line.md_type == :empty
    want_my_paragraph = true
    src.shift_line
  end

  raise "Chunky Bacon!" unless src.cur_line.md_type == :definition

  # Read one or more definitions
  definitions = []
  while src.cur_line && src.cur_line.md_type == :definition
    parent_offset = src.cur_index

    first = src.shift_line
    first =~ Definition
    first = $1

    lines, w_m_p = read_indented_content(src, 4, :definition, :definition)
    want_my_paragraph ||= w_m_p

    lines.unshift first

    src2 = LineSource.new(lines, src, parent_offset)
    children = parse_blocks(src2)
    definitions << md_el(:definition_data, children)
  end

  md_el(:definition, terms + definitions, {
          :terms => terms,
          :definitions => definitions,
          :want_my_paragraph => want_my_paragraph
        })
end

#read_footnote_text(src) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/maruku/input/parse_block.rb', line 344

def read_footnote_text(src)
  parent_offset = src.cur_index

  first = src.shift_line

  unless first =~ FootnoteText
    maruku_error "Bug (it's Andrea's fault)"
  end

  id = $1
  text = $2 || ''

  indentation = 4 #first.size-text.size

  #   puts "id =_#{id}_; text=_#{text}_ indent=#{indentation}"

  break_list = [:footnote_text, :ref_definition, :definition, :abbreviation]
  item_type = :footnote_text
  lines, _ = read_indented_content(src, indentation, break_list, item_type)

  # add first line
  lines.unshift text unless text.strip.empty?

  src2 = LineSource.new(lines, src, parent_offset)
  children = parse_blocks(src2)

  e = md_footnote(id, children)
  self.footnotes[id] = e
  e
end

#read_header12(src) ⇒ Object

reads a header (with —– or ========)



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/maruku/input/parse_block.rb', line 172

def read_header12(src)
  line = src.shift_line.strip
  al = nil
  # Check if there is an IAL
  if new_meta_data? and line =~ /^(.*?)\{(.*?)\}\s*$/
    line = $1.strip
    ial = $2
    al = read_attribute_list(CharSource.new(ial, src))
  end
  text = parse_span line
  if text.empty?
    text = "{#{ial}}"
    al = nil
  end
  level = src.cur_line.md_type == :header2 ? 2 : 1;
  src.shift_line
  md_header(level, text, al)
end

#read_header3(src) ⇒ Object

reads a header like ‘#### header ####’



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/maruku/input/parse_block.rb', line 192

def read_header3(src)
  line = src.shift_line.strip
  al = nil
  # Check if there is an IAL
  if new_meta_data? and line =~ /^(.*?)\{(.*?)\}\s*$/
    line = $1.strip
    ial = $2
    al = read_attribute_list(CharSource.new(ial, src))
  end
  level = line[/^#+/].size
  text = parse_span line.gsub(/\A#+|#+\z/, '')
  if text.empty?
    text = "{#{ial}}"
    al = nil
  end
  md_header(level, text, al)
end

#read_indented_content(src, indentation, break_list, item_type, ial_offset = 0) ⇒ Object

This is the only ugly function in the code base. It is used to read list items, descriptions, footnote text



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/maruku/input/parse_block.rb', line 378

def read_indented_content(src, indentation, break_list, item_type, ial_offset=0)
  lines = []
  # collect all indented lines
  saw_empty = false
  saw_anything_after = false
  break_list = Array(break_list)
  len = indentation - ial_offset

  while src.cur_line
    num_leading_spaces = src.cur_line.number_of_leading_spaces
    break if num_leading_spaces < len && ![:text, :empty].include?(src.cur_line.md_type)

    line = strip_indent(src.cur_line, indentation)
    md_type = line.md_type

    if md_type == :empty
      saw_empty = true
      lines << line
      src.shift_line
      next
    end

    # Unquestioningly grab anything that's deeper-indented
    if md_type != :code && num_leading_spaces > len
      lines << line
      src.shift_line
      next
    end

    # after a white line
    if saw_empty
      # we expect things to be properly aligned
      break if num_leading_spaces < len
      saw_anything_after = true
    else
      break if break_list.include?(md_type)
    end

    lines << line
    src.shift_line

    # You are only required to indent the first line of
    # a child paragraph.
    if md_type == :text
      while src.cur_line && src.cur_line.md_type == :text
        lines << strip_indent(src.shift_line, indentation)
      end
    end
  end

  # TODO fix this
  want_my_paragraph = saw_anything_after ||
    (saw_empty && src.cur_line && src.cur_line.md_type == item_type)

  # create a new context

  while lines.last && lines.last.md_type == :empty
    lines.pop
  end

  return lines, want_my_paragraph
end

#read_list_item(src) ⇒ Object

Reads one list item, either ordered or unordered.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/maruku/input/parse_block.rb', line 304

def read_list_item(src)
  parent_offset = src.cur_index

  item_type = src.cur_line.md_type
  first = src.shift_line

  indentation, ial = spaces_before_first_char(first)
  al = read_attribute_list(CharSource.new(ial, src)) if ial
  ial_offset = ial ? ial.length + 3 : 0
  lines, want_my_paragraph =
    read_indented_content(src, indentation, [], item_type, ial_offset)

  # add first line
  # Strip first '*', '-', '+' from first line
  stripped = first[indentation, first.size - 1]
  lines.unshift stripped

  src2 = LineSource.new(lines, src, parent_offset)
  children = parse_blocks(src2)

  md_li(children, want_my_paragraph, al)
end

#read_paragraph(src) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/maruku/input/parse_block.rb', line 272

def read_paragraph(src)
  lines = [src.shift_line]
  while src.cur_line
    # :olist does not break
    case t = src.cur_line.md_type
    when :quote, :header3, :empty, :ref_definition, :ial, :xml_instr
      break
    when :olist, :ulist
      break if !src.next_line || src.next_line.md_type == t
    when :raw_html
      # This is a pretty awful hack to handle inline HTML
      # but it means double-parsing HMTL.
      html = parse_span([src.cur_line], src)
      unless html.empty? || html.first.is_a?(String)
        if html.first.parsed_html
          first_node_name = html.first.parsed_html.first_node_name
        end
      end
      break if first_node_name && !HTML_INLINE_ELEMS.include?(first_node_name)
    end
    break if src.cur_line.strip.empty?
    break if src.next_line && [:header1, :header2].include?(src.next_line.md_type)
    break if any_matching_block_extension?(src.cur_line)

    lines << src.shift_line
  end
  children = parse_span(lines, src)

  md_par(children)
end

#read_quote(src) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/maruku/input/parse_block.rb', line 442

def read_quote(src)
  parent_offset = src.cur_index

  lines = []
  # collect all indented lines
  while src.cur_line && src.cur_line.md_type == :quote
    lines << unquote(src.shift_line)
  end

  src2 = LineSource.new(lines, src, parent_offset)
  children = parse_blocks(src2)
  md_quote(children)
end

#read_raw_html(src) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/maruku/input/parse_block.rb', line 239

def read_raw_html(src)
  extra_line = nil
  h = HTMLHelper.new
  begin
    l = src.shift_line
    h.eat_this(l)
    #     puts "\nBLOCK:\nhtml -> #{l.inspect}"
    while src.cur_line && !h.is_finished?
      l = src.shift_line
      #       puts "html -> #{l.inspect}"
      h.eat_this "\n" + l
    end
  rescue => e
    maruku_error "Bad block-level HTML:\n#{e.inspect.gsub(/^/, '|')}\n", src
  end
  unless h.rest =~ /^\s*$/
    extra_line = h.rest
  end
  raw_html = h.stuff_you_read

  is_inline = HTML_INLINE_ELEMS.include?(h.first_tag)

  if extra_line
    remainder = is_inline ? parse_span(extra_line) : parse_text_as_markdown(extra_line)
    if extra_line.start_with?(' ')
      remainder[0] = ' ' + remainder[0] if remainder[0].is_a?(String)
    end
    is_inline ? [md_html(raw_html), md_par(remainder)] : [md_html(raw_html)] + remainder
  else
    [md_html(raw_html)]
  end
end

#read_ref_definition(src, out) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/maruku/input/parse_block.rb', line 479

def read_ref_definition(src, out)
  line = src.shift_line

  # if link is incomplete, shift next line
  if src.cur_line &&
      ![:footnote_text, :ref_definition, :definition, :abbreviation].include?(src.cur_line.md_type) &&
      (1..3).include?(src.cur_line.number_of_leading_spaces)
    line << " " << src.shift_line
  end

  match = LinkRegex.match(line)
  unless match
    maruku_error "Link does not respect format: '#{line}'" and return
  end

  id = match[1]
  url = match[2]
  title = match[3] || match[4] || match[5]
  id = sanitize_ref_id(id)

  hash = self.refs[id] = {
    :url => url,
    :title => title
  }

  stuff = (match[6] || '')
  stuff.split.each do |couple|
    k, v = couple.split('=')
    v ||= ""
    v = v[1..-2] if v.start_with?('"') # strip quotes
    hash[k.to_sym] = v
  end

  out << md_ref_def(id, url, :title => title)
end

#read_table(src) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/maruku/input/parse_block.rb', line 519

def read_table(src)
  head = split_cells(src.shift_line).map do |s|
    md_el(:head_cell, parse_span(s))
  end

  separator = split_cells(src.shift_line)

  align = separator.map do |s|
    # ex: :-------------------:
    # If the separator starts and ends with a colon,
    # center the cell. If it's on the right, right-align,
    # otherwise left-align.
    starts = s.start_with? ':'
    ends = s.end_with? ':'
    if starts && ends
      :center
    elsif ends
      :right
    else
      :left
    end
  end

  num_columns = align.size

  if head.size != num_columns
    maruku_error "Table head does not have #{num_columns} columns: \n#{head.inspect}"
    tell_user "I will ignore this table."
    # XXX try to recover
    return md_br
  end

  rows = []

  while src.cur_line && src.cur_line =~ /\|/
    row = split_cells(src.shift_line).map do |s|
      md_el(:cell, parse_span(s))
    end

    if head.size != num_columns
      maruku_error  "Row does not have #{num_columns} columns: \n#{row.inspect}"
      tell_user "I will ignore this table."
      # XXX try to recover
      return md_br
    end
    rows << row
  end

  children = (head + rows).flatten
  md_el(:table, children, { :align => align })
end

#read_text_material(src, output) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/maruku/input/parse_block.rb', line 139

def read_text_material(src, output)
  if src.cur_line.include?('|') && # if contains a pipe, it could be a table header
      src.next_line &&
      src.next_line.rstrip =~ TableSeparator
    output << read_table(src)
  elsif src.next_line && [:header1, :header2].include?(src.next_line.md_type)
    output << read_header12(src)
  elsif eventually_comes_a_def_list(src)
    definition = read_definition(src)
    if output.last.kind_of?(MDElement) &&
        output.last.node_type == :definition_list then
      output.last.children << definition
    else
      output << md_el(:definition_list, definition)
    end
  else # Start of a paragraph
    output << read_paragraph(src)
  end
end

#read_xml_instruction(src, output) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/maruku/input/parse_block.rb', line 210

def read_xml_instruction(src, output)
  m = /^\s*<\?((\w+)\s*)?(.*)$/.match src.shift_line
  raise "BugBug" unless m
  target = m[2] || ''
  code = m[3]
  until code.include?('?>')
    code << "\n" << src.shift_line
  end
  unless code =~ /\?>\s*$/
    garbage = (/\?>(.*)$/.match(code))[1]
    maruku_error "Trailing garbage on last line: #{garbage.inspect}:\n" +
      code.gsub(/^/, '|'), src
  end
  code.gsub!(/\?>\s*$/, '')

  if target == 'mrk' && MaRuKu::Globals[:unsafe_features]
    result = safe_execute_code(self, code)
    if result
      if result.kind_of? String
        raise "Not expected"
      else
        output.push(*result)
      end
    end
  else
    output << md_xml_instr(target, code)
  end
end

#safe_execute_code(object, code) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/maruku/input/parse_doc.rb', line 111

def safe_execute_code(object, code)
  begin
    object.instance_eval(code)
  rescue StandardError, ScriptError => e
    maruku_error "Exception while executing this:\n" +
      code.gsub(/^/, ">") +
      "\nThe error was:\n" +
      (e.inspect + "\n" + e.caller.join("\n")).gsub(/^/, "|")
    nil
  end
end

#search_abbreviationsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/maruku/input/parse_doc.rb', line 134

def search_abbreviations
  abbreviations.each do |abbrev, title|
    reg = Regexp.new(Regexp.escape(abbrev))
    replace_each_string do |s|
      # bug if many abbreviations are present (agorf)
      p = StringScanner.new(s)
      a = []
      until p.eos?
        o = ''
        o << p.getch until p.scan(reg) or p.eos?
        a << o unless o.empty?
        a << md_abbr(abbrev.dup, title ? title.dup : nil) if p.matched == abbrev
      end
      a
    end
  end
end

#split_cells(s) ⇒ Object



515
516
517
# File 'lib/maruku/input/parse_block.rb', line 515

def split_cells(s)
  s.split('|').reject(&:empty?).map(&:strip)
end

#substitute_markdown_inside_raw_htmlObject

(PHP Markdown extra) Search for elements that have markdown=1 or markdown=block defined



154
155
156
157
158
159
160
161
# File 'lib/maruku/input/parse_doc.rb', line 154

def substitute_markdown_inside_raw_html
  each_element(:raw_html) do |e|
    html = e.parsed_html
    next unless html

    html.process_markdown_inside_elements(self)
  end
end