Class: MaRuKu::MDDocument

Inherits:
MDElement show all
Includes:
In::Markdown, In::Markdown::BlockLevelParser, In::Markdown::SpanLevelParser, Out::Latex::MDDocumentExtensions
Defined in:
lib/maruku/document.rb,
lib/maruku.rb,
lib/maruku/toc.rb,
lib/maruku/output/s5/to_s5.rb,
lib/maruku/output/to_latex.rb,
lib/maruku/ext/math/parsing.rb,
lib/maruku/output/to_markdown.rb,
lib/maruku/input_textile2/t2_parser.rb

Overview

This represents the whole document and holds global data.

Direct Known Subclasses

Maruku

Defined Under Namespace

Classes: Handling

Constant Summary collapse

T2_Handling =
{
	nil => Handling.new(:t2_block_paragraph, true),
	'p' => Handling.new(:t2_block_paragraph, true)
}

Constants included from In::Markdown::SpanLevelParser

In::Markdown::SpanLevelParser::CharSource, In::Markdown::SpanLevelParser::EscapedCharInInlineCode, In::Markdown::SpanLevelParser::EscapedCharInQuotes, In::Markdown::SpanLevelParser::EscapedCharInText, In::Markdown::SpanLevelParser::IgnoreWikiLinks

Constants included from Strings

Strings::TAB_SIZE

Constants included from In::Markdown

In::Markdown::BlockExtensions, In::Markdown::SpanExtensions, In::Markdown::SpanExtensionsTrigger

Constants inherited from MDElement

MaRuKu::MDElement::INSPECT_FORMS

Constants included from Errors

Errors::FRAME_WIDTH

Constants included from Out::Latex

Out::Latex::LATEX_ADD_SLASH, Out::Latex::LATEX_TO_CHARCODE, Out::Latex::Latex_preamble_enc_cjk, Out::Latex::Latex_preamble_enc_utf8, Out::Latex::OtherGoodies, Out::Latex::SAFE_CHARS

Constants included from Out::HTML

Out::HTML::HTML4Attributes, Out::HTML::Xhtml11_mathml2_svg11

Constants included from Out::Markdown

Out::Markdown::DefaultLineLength

Constants included from MaRuKu

Abbreviation, AttributeDefinitionList, BLOCK_TAGS, Definition, EMailAddress, FootnoteText, Globals, HTML_INLINE_ELEMS, IncompleteLink, InlineAttributeList, LinkRegex, MARUKU_URL, MarukuURL, S5_Fancy, S5_external, TableSeparator, VERSION, Version

Instance Attribute Summary collapse

Attributes included from Out::Latex::MDDocumentExtensions

#latex_required_packages

Attributes inherited from MDElement

#al, #attributes, #children, #doc, #meta_priv, #node_type

Instance Method Summary collapse

Methods included from Out::Latex::MDDocumentExtensions

#latex_require_package

Methods included from In::Markdown::BlockLevelParser

#count_columns, #element_is_non_inline_html?, #eventually_comes_a_def_list, #execute_code_blocks, #expand_attribute_list, #find_colspan, #parse_blocks, #parse_doc, #parse_text_as_markdown, #pick_apart_non_inline_html, #read_abbreviation, #read_ald, #read_code, #read_definition, #read_footnote_text, #read_header12, #read_header3, #read_indented_content, #read_list_item, #read_paragraph, #read_quote, #read_raw_html, #read_ref_definition, #read_table, #read_text_material, #read_xml_instruction, #safe_execute_code, #search_abbreviations, #split_cells, #substitute_markdown_inside_raw_html

Methods included from In::Markdown::SpanLevelParser

#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

Methods included from In::Markdown

#any_matching_block_extension?, #check_block_extensions, #check_span_extensions, register_block_extension, register_span_extension

Methods inherited from MDElement

#==, #children_to_s, #each_element, #generate_id, #get_setting, #inspect, #md_div, #md_equation, #md_inline_math, #replace_each_string, #to_s

Methods included from Errors

#maruku_error, #maruku_recover, #raise_error, #tell_user

Methods included from Out::Latex

#array_to_latex, #children_to_latex, #latex_color, #latex_escape, #render_latex_signature, #string_to_latex, #to_latex, #to_latex_abbr, #to_latex_cell, #to_latex_citation, #to_latex_code, #to_latex_definition, #to_latex_definition_list, #to_latex_div, #to_latex_divref, #to_latex_document, #to_latex_email_address, #to_latex_emphasis, #to_latex_entity, #to_latex_eqref, #to_latex_equation, #to_latex_footnote_reference, #to_latex_head_cell, #to_latex_header, #to_latex_hrule, #to_latex_im_link, #to_latex_image, #to_latex_immediate_link, #to_latex_inline_code, #to_latex_inline_math, #to_latex_li, #to_latex_linebreak, #to_latex_link, #to_latex_ol, #to_latex_paragraph, #to_latex_quote, #to_latex_raw_html, #to_latex_strong, #to_latex_table, #to_latex_ul, #wrap_as_environment, #wrap_as_span

Methods included from Out::HTML

#add_class_to, #add_css_to, #add_ws, #adjust_png, #array_to_html, #children_to_html, #convert_to_mathml_blahtex, #convert_to_mathml_itex2mml, #convert_to_mathml_none, #convert_to_mathml_ritex, #convert_to_png_blahtex, #convert_to_png_none, #day_suffix, escapeHTML, #html_element, #maruku_html_signature, #nice_date, #obfuscate, #pixels_per_ex, #render_footnotes, #render_mathml, #render_png, #render_section_number, #section_number, #to_html, #to_html_abbr, #to_html_cell, #to_html_citation, #to_html_code, #to_html_code_using_pre, #to_html_definition, #to_html_definition_data, #to_html_definition_list, #to_html_definition_term, #to_html_div, #to_html_divref, #to_html_document, #to_html_document_tree, #to_html_email_address, #to_html_emphasis, #to_html_entity, #to_html_eqref, #to_html_equation, #to_html_footnote_reference, #to_html_head_cell, #to_html_header, #to_html_hrule, #to_html_im_image, #to_html_im_link, #to_html_image, #to_html_immediate_link, #to_html_inline_code, #to_html_inline_math, #to_html_li, #to_html_linebreak, #to_html_link, #to_html_ol, #to_html_paragraph, #to_html_quote, #to_html_raw_html, #to_html_ref_definition, #to_html_strong, #to_html_table, #to_html_ul, #to_html_xml_instr, #to_latex_ref_definition, #wrap_as_element, #xelem, #xml_newline, #xtext

Methods included from Out::Markdown

#add_indent, #array_to_md, #children_to_md, #mysplit, #to_md_abbr_def, #to_md_code, #to_md_email_address, #to_md_emphasis, #to_md_entity, #to_md_header, #to_md_hrule, #to_md_im_image, #to_md_im_link, #to_md_image, #to_md_immediate_link, #to_md_inline_code, #to_md_linebreak, #to_md_link, #to_md_ol, #to_md_paragraph, #to_md_quote, #to_md_ref_definition, #to_md_strong, #to_md_ul, #wrap

Methods included from MaRuKu

#markdown_extra?, #new_meta_data?, textile2

Constructor Details

#initialize(s = nil) ⇒ MDDocument

Returns a new instance of MDDocument.



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

def initialize(s=nil)
  super(:document)

  self.doc = self
  self.refs = {}
  self.footnotes = {}
  self.footnotes_order = []
  self.abbreviations = {}
  self.ald = {}
  self.refid2ref = {}
  self.id_counter = 0

  parse_doc(s) if s
end

Instance Attribute Details

#abbreviations{String => String}

Returns:



11
12
13
# File 'lib/maruku/document.rb', line 11

def abbreviations
  @abbreviations
end

#ald{String => AttributeList}

Attribute definition lists.

Returns:



16
17
18
# File 'lib/maruku/document.rb', line 16

def ald
  @ald
end

#eqid2eqString => MDElement

A hash of equation ids to equation elements

Returns:



6
7
8
# File 'lib/maruku/ext/math/parsing.rb', line 6

def eqid2eq
  @eqid2eq
end

#footnotes{String => MDElement}

Returns:



8
9
10
# File 'lib/maruku/document.rb', line 8

def footnotes
  @footnotes
end

#footnotes_orderArray<String>

The order in which footnotes are used. Contains the id.

Returns:



21
22
23
# File 'lib/maruku/document.rb', line 21

def footnotes_order
  @footnotes_order
end

#header_idsHash<String, Number>

A map of header IDs to a count of how many times they’ve occurred in the document.

Returns:



131
132
133
# File 'lib/maruku/toc.rb', line 131

def header_ids
  @header_ids
end

#id_counterObject

A counter for generating unique IDs [Integer]



27
28
29
# File 'lib/maruku/document.rb', line 27

def id_counter
  @id_counter
end

#refid2ref{String => {String => MDElement}}

Returns:



24
25
26
# File 'lib/maruku/document.rb', line 24

def refid2ref
  @refid2ref
end

#refs{String => {:url => String, :title => String}}

Returns:



5
6
7
# File 'lib/maruku/document.rb', line 5

def refs
  @refs
end

#tocSection

The table of contents for the document.

Returns:



126
127
128
# File 'lib/maruku/toc.rb', line 126

def toc
  @toc
end

Instance Method Details

#create_tocObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/maruku/toc.rb', line 133

def create_toc
  self.header_ids = Hash.new(0)

  each_element(:header) {|h| h.attributes[:id] ||= h.generate_id }


  # The root section
  s = Section.new
  s.section_level = 0

  stack = [s]

  i = 0
  while i < @children.size
    if children[i].node_type == :header
      header = @children[i]
      level = header.level
      s2 = Section.new
      s2.section_level = level
      s2.header_element = header
      header.instance_variable_set :@section, s2
      while level <= stack.last.section_level
        stack.pop
      end
      stack.last.section_children.push s2
      stack.push s2
    else
      stack.last.immediate_children.push @children[i]
    end
    i += 1
  end

  # If there is only one big header, then assume
  # it is the master
  if s.section_children.size == 1
    s = s.section_children.first
  end

  # Assign section numbers
  s.numerate

  s
end

#is_math_enabled?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/maruku/ext/math/parsing.rb', line 8

def is_math_enabled?
  get_setting :math_enabled
end

#old_mdObject



203
# File 'lib/maruku/output/to_markdown.rb', line 203

alias old_md to_md

#s5_themeObject



5
6
7
# File 'lib/maruku/output/s5/to_s5.rb', line 5

def s5_theme
  xtext(self.attributes[:slide_theme] || "default")
end

#t2_block_paragraph(src, output, signature, lines) ⇒ Object



151
152
153
154
155
# File 'lib/maruku/input_textile2/t2_parser.rb', line 151

def t2_block_paragraph(src, output, signature, lines)
	paragraph = lines.join("\n")
	src2 = CharSource.new(paragraph, src)
#			output = 
end

#t2_parse(source, params) ⇒ Object



95
96
97
98
99
100
# File 'lib/maruku/input_textile2/t2_parser.rb', line 95

def t2_parse(source, params)
	src = LineSource.new(source)
	output = BlockContext.new
	t2_parse_blocks(src, output)
	self.children = output.elements
end

#t2_parse_blocks(src, output) ⇒ Object

Input is a LineSource



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
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/maruku/input_textile2/t2_parser.rb', line 109

def t2_parse_blocks(src, output)
	while src.cur_line
		l = src.shift_line
		
		# ignore empty line
		if l.t2_empty? then 
			src.shift_line
			next 
		end
		
		# TODO: lists
		# TODO: xml
		# TODO: `==`

		signature, l =
			if l.t2_contains_signature?
				l.t2_get_signature
			else
				[Textile2Signature.new, l]
			end

		if handling = T2_Handling.has_key?(signature.block_name)
			if self.responds_to? handling.method
				# read as many non-empty lines that you can
				lines = [l]
				if handling.parse_lines
					while not src.cur_line.t2_empty?
						lines.push src.shift_line
					end
				end
			
				self.send(handling.method, src, output, signature, lines)
			else
				maruku_error("We don't know about method #{handling.method.inspect}")
				next
			end
		end
		
		
	end
end

#t2_parse_span(src, output) ⇒ Object



157
158
159
# File 'lib/maruku/input_textile2/t2_parser.rb', line 157

def t2_parse_span(src, output)
	
end

#to_md(context = {}) ⇒ Object



204
205
206
207
# File 'lib/maruku/output/to_markdown.rb', line 204

def to_md(context={})
  warn "Maruku#to_md is deprecated and will be removed in a near-future version of Maruku."
  old_md(context)
end

#to_s5(context = {}) ⇒ Object

Render as an HTML fragment (no head, just the content of BODY). (returns a string)



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
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
# File 'lib/maruku/output/s5/to_s5.rb', line 10

def to_s5(context={})
  content_only = context[:content_only] != false
  print_slides = context[:print_slides]

  if content_only
    body = xelem('div', doc)
  else
    html = xelem('html')
    html['xmlns'] = 'http://www.w3.org/1999/xhtml'
    html['xmlns:svg'] = "http://www.w3.org/2000/svg"
    html['xml:lang'] = self.attributes[:lang] || 'en'

    head = xelem('head')
    html << head

    me = xelem('meta')
    me['http-equiv'] = 'Content-type'
    me['content'] = 'text/html;charset=utf-8'
    head << me

    # Create title element
    doc_title = self.attributes[:title] || self.attributes[:subject] || ""
    begin
      title_content = MaRuKu::HTMLFragment.new(doc_title).to_html
    rescue
      title_content = xtext(doc_title)
    end
    title = xelem('title') << title_content
    head << title

    body = xelem('body')
    html << body
  end

  slide_header = self.attributes[:slide_header]
  slide_footer = self.attributes[:slide_footer]
  slide_subfooter = self.attributes[:slide_subfooter]
  slide_topleft  = self.attributes[:slide_topleft]
  slide_topright  = self.attributes[:slide_topright]
  slide_bottomleft  = self.attributes[:slide_bottomleft]
  slide_bottomright  = self.attributes[:slide_bottomright]

  dummy_layout_slide = "
<div class='layout'>
<div id='controls'> </div>
<div id='currentSlide'> </div>
<div id='header'> #{slide_header}</div>
<div id='footer'>
<h1>#{slide_footer}</h1>
<h2>#{slide_subfooter}</h2>
</div>
<div class='topleft'> #{slide_topleft}</div>
<div class='topright'> #{slide_topright}</div>
<div class='bottomleft'> #{slide_bottomleft}</div>
<div class='bottomright'> #{slide_bottomright}</div>
</div>
            "
  body <<  dummy_layout_slide

  presentation = xelem('div')
  presentation['class'] = 'presentation'
  body << presentation

  first_slide = "
<div class='slide'>
<h1> #{self.attributes[:title] ||context[:title]}</h1>
<h2> #{self.attributes[:subtitle] ||context[:subtitle]}</h2>
<h3> #{self.attributes[:author] ||context[:author]}</h3>
<h4> #{self.attributes[:company] ||context[:company]}</h4>
</div>
"
  presentation << first_slide

  slide_num = 0
  self.toc.section_children.each do |slide|
    slide_num += 1
    @doc.attributes[:doc_prefix] = "s#{slide_num}"

    div = xelem('div')
    presentation << div
    div['class'] = 'slide'

    h1 = xelem('h1')
    puts "Slide #{slide_num}: #{slide.header_element.children_to_html.join}" if print_slides
    slide.header_element.children_to_html.inject(h1, &:<<)
    div << h1

    array_to_html(slide.immediate_children).inject(div, &:<<)

    # render footnotes
    unless @doc.footnotes_order.empty?
      div << render_footnotes
      @doc.footnotes_order = []
    end
  end

  if content_only
    xml = body.to_html
  else
    head << S5_external

    add_css_to(head)

    xml = html.to_html
    Xhtml11_mathml2_svg11 + xml
  end
end