patcito-maruku 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/AUTHORS +23 -0
- data/LICENSE +340 -0
- data/README.md +73 -0
- data/bin/maruku +196 -0
- data/bin/marutex +4 -0
- data/data/entities.xml +261 -0
- data/docs/changelog.md +334 -0
- data/docs/div_syntax.md +36 -0
- data/docs/entity_test.md +23 -0
- data/docs/markdown_syntax.md +899 -0
- data/docs/maruku.md +346 -0
- data/docs/math.md +194 -0
- data/docs/other_stuff.md +51 -0
- data/docs/proposal.md +309 -0
- data/docs/website/src/bluecloth.md +25 -0
- data/docs/website/src/download.md +31 -0
- data/docs/website/src/maruku.md +261 -0
- data/docs/website/src/proposal.md +271 -0
- data/lib/maruku.rb +132 -0
- data/lib/maruku/attributes.rb +138 -0
- data/lib/maruku/defaults.rb +69 -0
- data/lib/maruku/errors.rb +89 -0
- data/lib/maruku/ext/div.rb +121 -0
- data/lib/maruku/ext/fenced_code.rb +78 -0
- data/lib/maruku/ext/math.rb +37 -0
- data/lib/maruku/ext/math/elements.rb +21 -0
- data/lib/maruku/ext/math/latex_fix.rb +12 -0
- data/lib/maruku/ext/math/mathml_engines/blahtex.rb +93 -0
- data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +39 -0
- data/lib/maruku/ext/math/mathml_engines/none.rb +21 -0
- data/lib/maruku/ext/math/mathml_engines/ritex.rb +24 -0
- data/lib/maruku/ext/math/parsing.rb +125 -0
- data/lib/maruku/ext/math/to_html.rb +237 -0
- data/lib/maruku/ext/math/to_latex.rb +36 -0
- data/lib/maruku/ext/yaml.rb +43 -0
- data/lib/maruku/helpers.rb +214 -0
- data/lib/maruku/input/charsource.rb +326 -0
- data/lib/maruku/input/extensions.rb +69 -0
- data/lib/maruku/input/html_helper.rb +189 -0
- data/lib/maruku/input/linesource.rb +111 -0
- data/lib/maruku/input/parse_block.rb +608 -0
- data/lib/maruku/input/parse_doc.rb +240 -0
- data/lib/maruku/input/parse_span_better.rb +746 -0
- data/lib/maruku/input/rubypants.rb +225 -0
- data/lib/maruku/input/type_detection.rb +147 -0
- data/lib/maruku/input_textile2/t2_parser.rb +163 -0
- data/lib/maruku/maruku.rb +31 -0
- data/lib/maruku/output/s5/fancy.rb +756 -0
- data/lib/maruku/output/s5/to_s5.rb +138 -0
- data/lib/maruku/output/to_html.rb +994 -0
- data/lib/maruku/output/to_latex.rb +580 -0
- data/lib/maruku/output/to_latex_entities.rb +101 -0
- data/lib/maruku/output/to_latex_strings.rb +64 -0
- data/lib/maruku/output/to_markdown.rb +164 -0
- data/lib/maruku/output/to_s.rb +54 -0
- data/lib/maruku/string_utils.rb +185 -0
- data/lib/maruku/structures.rb +143 -0
- data/lib/maruku/structures_inspect.rb +51 -0
- data/lib/maruku/structures_iterators.rb +48 -0
- data/lib/maruku/textile2.rb +1 -0
- data/lib/maruku/toc.rb +214 -0
- data/lib/maruku/usage/example1.rb +33 -0
- data/lib/maruku/version +0 -0
- data/lib/maruku/version.rb +54 -0
- data/spec/block_docs/abbreviations.md +52 -0
- data/spec/block_docs/alt.md +17 -0
- data/spec/block_docs/attributes/att2.md +20 -0
- data/spec/block_docs/attributes/att3.md +28 -0
- data/spec/block_docs/attributes/attributes.md +57 -0
- data/spec/block_docs/attributes/circular.md +26 -0
- data/spec/block_docs/attributes/default.md +22 -0
- data/spec/block_docs/blank.md +24 -0
- data/spec/block_docs/blanks_in_code.md +75 -0
- data/spec/block_docs/bug_def.md +16 -0
- data/spec/block_docs/bug_table.md +46 -0
- data/spec/block_docs/code.md +34 -0
- data/spec/block_docs/code2.md +28 -0
- data/spec/block_docs/code3.md +71 -0
- data/spec/block_docs/data_loss.md +25 -0
- data/spec/block_docs/divs/div1.md +167 -0
- data/spec/block_docs/divs/div2.md +21 -0
- data/spec/block_docs/divs/div3_nest.md +45 -0
- data/spec/block_docs/easy.md +15 -0
- data/spec/block_docs/email.md +20 -0
- data/spec/block_docs/encoding/iso-8859-1.md +23 -0
- data/spec/block_docs/encoding/utf-8.md +18 -0
- data/spec/block_docs/entities.md +94 -0
- data/spec/block_docs/escaping.md +67 -0
- data/spec/block_docs/extra_dl.md +52 -0
- data/spec/block_docs/extra_header_id.md +63 -0
- data/spec/block_docs/extra_table1.md +37 -0
- data/spec/block_docs/footnotes.md +97 -0
- data/spec/block_docs/headers.md +37 -0
- data/spec/block_docs/hex_entities.md +37 -0
- data/spec/block_docs/hrule.md +39 -0
- data/spec/block_docs/html2.md +22 -0
- data/spec/block_docs/html3.md +31 -0
- data/spec/block_docs/html4.md +25 -0
- data/spec/block_docs/html5.md +23 -0
- data/spec/block_docs/ie.md +49 -0
- data/spec/block_docs/images.md +90 -0
- data/spec/block_docs/images2.md +31 -0
- data/spec/block_docs/inline_html.md +152 -0
- data/spec/block_docs/inline_html2.md +21 -0
- data/spec/block_docs/links.md +152 -0
- data/spec/block_docs/links2.md +22 -0
- data/spec/block_docs/list1.md +46 -0
- data/spec/block_docs/list12.md +28 -0
- data/spec/block_docs/list2.md +56 -0
- data/spec/block_docs/list3.md +64 -0
- data/spec/block_docs/list4.md +89 -0
- data/spec/block_docs/lists.md +192 -0
- data/spec/block_docs/lists10.md +34 -0
- data/spec/block_docs/lists11.md +23 -0
- data/spec/block_docs/lists6.md +41 -0
- data/spec/block_docs/lists9.md +64 -0
- data/spec/block_docs/lists_after_paragraph.md +208 -0
- data/spec/block_docs/lists_ol.md +262 -0
- data/spec/block_docs/loss.md +16 -0
- data/spec/block_docs/math/equations.md +45 -0
- data/spec/block_docs/math/inline.md +46 -0
- data/spec/block_docs/math/math2.md +45 -0
- data/spec/block_docs/math/notmath.md +25 -0
- data/spec/block_docs/math/table.md +25 -0
- data/spec/block_docs/math/table2.md +42 -0
- data/spec/block_docs/misc_sw.md +525 -0
- data/spec/block_docs/notyet/escape.md +21 -0
- data/spec/block_docs/notyet/header_after_par.md +58 -0
- data/spec/block_docs/notyet/ticks.md +18 -0
- data/spec/block_docs/notyet/triggering.md +157 -0
- data/spec/block_docs/olist.md +45 -0
- data/spec/block_docs/one.md +15 -0
- data/spec/block_docs/paragraph.md +16 -0
- data/spec/block_docs/paragraph_rules/dont_merge_ref.md +42 -0
- data/spec/block_docs/paragraph_rules/tab_is_blank.md +24 -0
- data/spec/block_docs/paragraphs.md +46 -0
- data/spec/block_docs/pending/amps.md +15 -0
- data/spec/block_docs/pending/empty_cells.md +37 -0
- data/spec/block_docs/pending/link.md +72 -0
- data/spec/block_docs/pending/ref.md +21 -0
- data/spec/block_docs/recover/recover_links.md +15 -0
- data/spec/block_docs/red_tests/abbrev.md +679 -0
- data/spec/block_docs/red_tests/lists7.md +32 -0
- data/spec/block_docs/red_tests/lists7b.md +65 -0
- data/spec/block_docs/red_tests/lists8.md +42 -0
- data/spec/block_docs/red_tests/ref.md +23 -0
- data/spec/block_docs/red_tests/xml.md +35 -0
- data/spec/block_docs/references/long_example.md +71 -0
- data/spec/block_docs/references/spaces_and_numbers.md +15 -0
- data/spec/block_docs/smartypants.md +114 -0
- data/spec/block_docs/syntax_hl.md +52 -0
- data/spec/block_docs/table_attributes.md +34 -0
- data/spec/block_docs/test.md +19 -0
- data/spec/block_docs/underscore_in_words.md +15 -0
- data/spec/block_docs/wrapping.md +67 -0
- data/spec/block_docs/xml2.md +19 -0
- data/spec/block_docs/xml3.md +26 -0
- data/spec/block_docs/xml_instruction.md +52 -0
- data/spec/block_spec.rb +49 -0
- data/spec/span_spec.rb +254 -0
- data/spec/spec_helper.rb +6 -0
- metadata +247 -0
@@ -0,0 +1,143 @@
|
|
1
|
+
# Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
|
2
|
+
#
|
3
|
+
# This file is part of Maruku.
|
4
|
+
#
|
5
|
+
# Maruku is free software; you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation; either version 2 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Maruku is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Maruku; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
|
19
|
+
|
20
|
+
module MaRuKu
|
21
|
+
# Rather than having a separate class for every possible element,
|
22
|
+
# Maruku has a single {MDElement} class
|
23
|
+
# that represents eveything in the document (paragraphs, headers, etc).
|
24
|
+
# The type of each element is available via \{#node\_type}.
|
25
|
+
class MDElement
|
26
|
+
# The type of this node (e.g. `:quote`, `:image`, `:abbr`).
|
27
|
+
# See {Helpers} for a list of possible values.
|
28
|
+
#
|
29
|
+
# @return [Symbol]
|
30
|
+
attr_accessor :node_type
|
31
|
+
|
32
|
+
# The child nodes of this element.
|
33
|
+
#
|
34
|
+
# @return [Array<String or MDElement>]
|
35
|
+
attr_accessor :children
|
36
|
+
|
37
|
+
# An attribute list. May not be nil.
|
38
|
+
#
|
39
|
+
# @return [AttributeList]
|
40
|
+
attr_accessor :al
|
41
|
+
|
42
|
+
# The processed attributes.
|
43
|
+
#
|
44
|
+
# For the {Maruku document root},
|
45
|
+
# this contains properties listed
|
46
|
+
# at the beginning of the document.
|
47
|
+
# The properties will be downcased and any spaces
|
48
|
+
# will be converted to underscores.
|
49
|
+
# For example, if you write in the source document:
|
50
|
+
#
|
51
|
+
# !!!text
|
52
|
+
# Title: test document
|
53
|
+
# My property: value
|
54
|
+
#
|
55
|
+
# content content
|
56
|
+
#
|
57
|
+
# Then \{#attributes} will return:
|
58
|
+
#
|
59
|
+
# {:title => "test document", :my_property => "value"}
|
60
|
+
#
|
61
|
+
# @return [{Symbol => String}]
|
62
|
+
attr_accessor :attributes
|
63
|
+
|
64
|
+
# The root element of the document
|
65
|
+
# to which this element belongs.
|
66
|
+
#
|
67
|
+
# @return [Maruku]
|
68
|
+
attr_accessor :doc
|
69
|
+
|
70
|
+
def initialize(node_type = :unset, children = [], meta = {}, al = nil)
|
71
|
+
self.children = children
|
72
|
+
self.node_type = node_type
|
73
|
+
self.attributes = {}
|
74
|
+
|
75
|
+
meta.each do |symbol, value|
|
76
|
+
self.instance_eval <<RUBY
|
77
|
+
def #{symbol}; @#{symbol}; end
|
78
|
+
def #{symbol}=(val); @#{symbol} = val; end
|
79
|
+
RUBY
|
80
|
+
self.send "#{symbol}=", value
|
81
|
+
end
|
82
|
+
|
83
|
+
self.al = al || AttributeList.new
|
84
|
+
self.meta_priv = meta
|
85
|
+
end
|
86
|
+
|
87
|
+
# @private
|
88
|
+
attr_accessor :meta_priv
|
89
|
+
|
90
|
+
def ==(o)
|
91
|
+
o.is_a?(MDElement) &&
|
92
|
+
self.node_type == o.node_type &&
|
93
|
+
self.meta_priv == o.meta_priv &&
|
94
|
+
self.children == o.children
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# This represents the whole document and holds global data.
|
99
|
+
class MDDocument
|
100
|
+
# @return [{String => {:url => String, :title => String}}]
|
101
|
+
attr_accessor :refs
|
102
|
+
|
103
|
+
# @return [{String => MDElement}]
|
104
|
+
attr_accessor :footnotes
|
105
|
+
|
106
|
+
# @return [{String => String}]
|
107
|
+
attr_accessor :abbreviations
|
108
|
+
|
109
|
+
# Attribute definition lists.
|
110
|
+
#
|
111
|
+
# @return [{String => AttributeList}]
|
112
|
+
attr_accessor :ald
|
113
|
+
|
114
|
+
# The order in which footnotes are used. Contains the id.
|
115
|
+
#
|
116
|
+
# @return [Array<String>]
|
117
|
+
attr_accessor :footnotes_order
|
118
|
+
|
119
|
+
# @return [Array<String>]
|
120
|
+
attr_accessor :latex_required_packages
|
121
|
+
|
122
|
+
# @return [{String => {String => MDElement}}]
|
123
|
+
attr_accessor :refid2ref
|
124
|
+
|
125
|
+
# A counter for generating unique IDs [Integer]
|
126
|
+
attr_accessor :id_counter
|
127
|
+
|
128
|
+
def initialize(s=nil)
|
129
|
+
super(:document)
|
130
|
+
|
131
|
+
self.doc = self
|
132
|
+
self.refs = {}
|
133
|
+
self.footnotes = {}
|
134
|
+
self.footnotes_order = []
|
135
|
+
self.abbreviations = {}
|
136
|
+
self.ald = {}
|
137
|
+
self.latex_required_packages = []
|
138
|
+
self.id_counter = 0
|
139
|
+
|
140
|
+
parse_doc(s) if s
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
|
2
|
+
#
|
3
|
+
# This file is part of Maruku.
|
4
|
+
#
|
5
|
+
# Maruku is free software; you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation; either version 2 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Maruku is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Maruku; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
|
19
|
+
|
20
|
+
module MaRuKu
|
21
|
+
class MDElement
|
22
|
+
def inspect(compact=true)
|
23
|
+
if compact
|
24
|
+
i2 = inspect2
|
25
|
+
return i2 if i2
|
26
|
+
end
|
27
|
+
|
28
|
+
# Make sure the attributes are lexically ordered
|
29
|
+
meta_ordered = "{" + @meta_priv.keys.
|
30
|
+
map {|x| x.to_s}.sort.map {|x| x.to_sym}.
|
31
|
+
map {|k| k.inspect + "=>" + @meta_priv[k].inspect}.
|
32
|
+
join(',') + "}"
|
33
|
+
|
34
|
+
"md_el(%s,%s,%s,%s)" % [
|
35
|
+
self.node_type.inspect,
|
36
|
+
children_inspect(compact),
|
37
|
+
meta_ordered,
|
38
|
+
self.al.inspect
|
39
|
+
]
|
40
|
+
end
|
41
|
+
|
42
|
+
def children_inspect(compact=true)
|
43
|
+
kids = @children.map {|x| x.is_a?(MDElement) ? x.inspect(compact) : x.inspect}
|
44
|
+
comma = kids.join(", ")
|
45
|
+
|
46
|
+
return "[#{comma}]" if comma.size < 70
|
47
|
+
"[\n\t#{kids.join(",\n\t")}\n]"
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
|
2
|
+
#
|
3
|
+
# This file is part of Maruku.
|
4
|
+
#
|
5
|
+
# Maruku is free software; you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation; either version 2 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Maruku is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Maruku; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
|
19
|
+
|
20
|
+
module MaRuKu
|
21
|
+
class MDElement
|
22
|
+
# Iterates through each {MDElement} child node of this element.
|
23
|
+
# This includes deeply-nested child nodes.
|
24
|
+
# If `e_node_type` is specified, only yields nodes of that type.
|
25
|
+
def each_element(e_node_type=nil, &block)
|
26
|
+
@children.each do |c|
|
27
|
+
next unless c.is_a? MDElement
|
28
|
+
yield c if e_node_type.nil? || c.node_type == e_node_type
|
29
|
+
c.each_element(e_node_type, &block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Iterates through each String child node of this element,
|
34
|
+
# replacing it with the result of the block.
|
35
|
+
# This includes deeply-nested child nodes.
|
36
|
+
#
|
37
|
+
# This destructively modifies this node and its children.
|
38
|
+
#
|
39
|
+
# @todo Make this non-destructive
|
40
|
+
def replace_each_string(&block)
|
41
|
+
@children.map! do |c|
|
42
|
+
next yield c unless c.is_a?(MDElement)
|
43
|
+
c.replace_each_string(&block)
|
44
|
+
c
|
45
|
+
end.flatten!
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'maruku/input_textile2/t2_parser'
|
data/lib/maruku/toc.rb
ADDED
@@ -0,0 +1,214 @@
|
|
1
|
+
# Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
|
2
|
+
#
|
3
|
+
# This file is part of Maruku.
|
4
|
+
#
|
5
|
+
# Maruku is free software; you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation; either version 2 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Maruku is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Maruku; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
|
19
|
+
|
20
|
+
module MaRuKu
|
21
|
+
# A section in the table of contents of a document.
|
22
|
+
class Section
|
23
|
+
# The depth of the section (0 for toplevel).
|
24
|
+
#
|
25
|
+
# Equivalent to `header_element.level`.
|
26
|
+
#
|
27
|
+
# @return [Fixnum]
|
28
|
+
attr_accessor :section_level
|
29
|
+
|
30
|
+
# The nested section number, e.g. `[1, 2, 5]` for Section 1.2.5.
|
31
|
+
#
|
32
|
+
# @return [Array<Fixnum>]
|
33
|
+
attr_accessor :section_number
|
34
|
+
|
35
|
+
# The `:header` node for this section.
|
36
|
+
# The value of `meta[:section]` for the header will be this node.
|
37
|
+
#
|
38
|
+
# @return [MDElement]
|
39
|
+
attr_accessor :header_element
|
40
|
+
|
41
|
+
# The immediate child nodes of this section.
|
42
|
+
#
|
43
|
+
# @todo Why does this never contain Strings?
|
44
|
+
#
|
45
|
+
# @return [Array<MDElement>]
|
46
|
+
attr_accessor :immediate_children
|
47
|
+
|
48
|
+
# The subsections of this section.
|
49
|
+
#
|
50
|
+
# @return [Array<Section>]
|
51
|
+
attr_accessor :section_children
|
52
|
+
|
53
|
+
def initialize
|
54
|
+
@immediate_children = []
|
55
|
+
@section_children = []
|
56
|
+
end
|
57
|
+
|
58
|
+
def inspect(indent = 1)
|
59
|
+
s = ""
|
60
|
+
|
61
|
+
if @header_element
|
62
|
+
s << "\_" * indent <<
|
63
|
+
"(#{@section_level})>\t #{@section_number.join('.')} : " <<
|
64
|
+
@header_element.children_to_s <<
|
65
|
+
" (id: '#{@header_element.attributes[:id]}')\n"
|
66
|
+
else
|
67
|
+
s << "Master\n"
|
68
|
+
end
|
69
|
+
@section_children.each {|c| s << c.inspect(indent+1)}
|
70
|
+
|
71
|
+
s
|
72
|
+
end
|
73
|
+
|
74
|
+
# Assign \{#section\_number section numbers}
|
75
|
+
# to this section and its children.
|
76
|
+
# This also assigns the section number attribute
|
77
|
+
# to the sections' headers.
|
78
|
+
#
|
79
|
+
# This should only be called on the root section.
|
80
|
+
#
|
81
|
+
# @overload def numerate
|
82
|
+
def numerate(a = [])
|
83
|
+
self.section_number = a
|
84
|
+
section_children.each_with_index {|c, i| c.numerate(a + [i + 1])}
|
85
|
+
if h = self.header_element
|
86
|
+
h.attributes[:section_number] = self.section_number
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
include REXML
|
91
|
+
|
92
|
+
# Returns an HTML representation of the table of contents.
|
93
|
+
#
|
94
|
+
# This should only be called on the root section.
|
95
|
+
def to_html
|
96
|
+
div = Element.new 'div'
|
97
|
+
div.attributes['class'] = 'maruku_toc'
|
98
|
+
div << _to_html
|
99
|
+
div
|
100
|
+
end
|
101
|
+
|
102
|
+
# Returns a LaTeX representation of the table of contents.
|
103
|
+
#
|
104
|
+
# This should only be called on the root section.
|
105
|
+
def to_latex
|
106
|
+
_to_latex + "\n\n"
|
107
|
+
end
|
108
|
+
|
109
|
+
protected
|
110
|
+
|
111
|
+
def _to_html
|
112
|
+
ul = Element.new 'ul'
|
113
|
+
# let's remove the bullets
|
114
|
+
ul.attributes['style'] = 'list-style: none;'
|
115
|
+
@section_children.each do |c|
|
116
|
+
li = Element.new 'li'
|
117
|
+
if span = c.header_element.render_section_number
|
118
|
+
li << span
|
119
|
+
end
|
120
|
+
|
121
|
+
a = c.header_element.wrap_as_element('a')
|
122
|
+
a.delete_attribute 'id'
|
123
|
+
a.attributes['href'] = "##{c.header_element.attributes[:id]}"
|
124
|
+
|
125
|
+
li << a
|
126
|
+
li << c._to_html if c.section_children.size > 0
|
127
|
+
ul << li
|
128
|
+
end
|
129
|
+
ul
|
130
|
+
end
|
131
|
+
|
132
|
+
def _to_latex
|
133
|
+
s = ""
|
134
|
+
@section_children.each do |c|
|
135
|
+
s << "\\noindent"
|
136
|
+
if number = c.header_element.section_number
|
137
|
+
s << number
|
138
|
+
end
|
139
|
+
id = c.header_element.attributes[:id]
|
140
|
+
text = c.header_element.children_to_latex
|
141
|
+
s << "\\hyperlink{#{id}}{#{text}}"
|
142
|
+
s << "\\dotfill \\pageref*{#{id}} \\linebreak\n"
|
143
|
+
s << c._to_latex if c.section_children.size > 0
|
144
|
+
end
|
145
|
+
s
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
class MDDocument
|
150
|
+
# The table of contents for the document.
|
151
|
+
#
|
152
|
+
# @return [Section]
|
153
|
+
attr_accessor :toc
|
154
|
+
|
155
|
+
def create_toc
|
156
|
+
each_element(:header) {|h| h.attributes[:id] ||= h.generate_id}
|
157
|
+
|
158
|
+
stack = []
|
159
|
+
|
160
|
+
# The root section
|
161
|
+
s = Section.new
|
162
|
+
s.section_level = 0
|
163
|
+
|
164
|
+
stack.push s
|
165
|
+
|
166
|
+
# TODO: Clean up the logic here once we have better tests
|
167
|
+
i = 0
|
168
|
+
while i < @children.size
|
169
|
+
while i < @children.size
|
170
|
+
if @children[i].node_type == :header
|
171
|
+
level = @children[i].level
|
172
|
+
break if level <= stack.last.section_level + 1
|
173
|
+
end
|
174
|
+
|
175
|
+
stack.last.immediate_children.push @children[i]
|
176
|
+
i += 1
|
177
|
+
end
|
178
|
+
|
179
|
+
break if i >= @children.size
|
180
|
+
|
181
|
+
header = @children[i]
|
182
|
+
level = header.level
|
183
|
+
|
184
|
+
if level > stack.last.section_level
|
185
|
+
# this level is inside
|
186
|
+
|
187
|
+
s2 = Section.new
|
188
|
+
s2.section_level = level
|
189
|
+
s2.header_element = header
|
190
|
+
header.instance_variable_set :@section, s2
|
191
|
+
|
192
|
+
stack.last.section_children.push s2
|
193
|
+
stack.push s2
|
194
|
+
|
195
|
+
i += 1
|
196
|
+
elsif level == stack.last.section_level
|
197
|
+
# this level is a sibling
|
198
|
+
stack.pop
|
199
|
+
else
|
200
|
+
# this level is a parent
|
201
|
+
stack.pop
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# If there is only one big header, then assume it is the master
|
206
|
+
s = s.section_children.first if s.section_children.size == 1
|
207
|
+
|
208
|
+
# Assign section numbers
|
209
|
+
s.numerate
|
210
|
+
|
211
|
+
s
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|