bean-kramdown 0.13.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/AUTHORS +1 -0
  2. data/CONTRIBUTERS +11 -0
  3. data/COPYING +24 -0
  4. data/ChangeLog +6683 -0
  5. data/GPL +674 -0
  6. data/README +43 -0
  7. data/VERSION +1 -0
  8. data/bin/kramdown +78 -0
  9. data/lib/kramdown.rb +23 -0
  10. data/lib/kramdown/compatibility.rb +49 -0
  11. data/lib/kramdown/converter.rb +41 -0
  12. data/lib/kramdown/converter/base.rb +169 -0
  13. data/lib/kramdown/converter/bean_html.rb +71 -0
  14. data/lib/kramdown/converter/html.rb +411 -0
  15. data/lib/kramdown/converter/kramdown.rb +428 -0
  16. data/lib/kramdown/converter/latex.rb +607 -0
  17. data/lib/kramdown/converter/toc.rb +82 -0
  18. data/lib/kramdown/document.rb +119 -0
  19. data/lib/kramdown/element.rb +524 -0
  20. data/lib/kramdown/error.rb +30 -0
  21. data/lib/kramdown/options.rb +373 -0
  22. data/lib/kramdown/parser.rb +39 -0
  23. data/lib/kramdown/parser/base.rb +136 -0
  24. data/lib/kramdown/parser/bean_kramdown.rb +25 -0
  25. data/lib/kramdown/parser/bean_kramdown/info_box.rb +52 -0
  26. data/lib/kramdown/parser/bean_kramdown/oembed.rb +230 -0
  27. data/lib/kramdown/parser/html.rb +570 -0
  28. data/lib/kramdown/parser/kramdown.rb +339 -0
  29. data/lib/kramdown/parser/kramdown/abbreviation.rb +71 -0
  30. data/lib/kramdown/parser/kramdown/autolink.rb +53 -0
  31. data/lib/kramdown/parser/kramdown/blank_line.rb +43 -0
  32. data/lib/kramdown/parser/kramdown/block_boundary.rb +46 -0
  33. data/lib/kramdown/parser/kramdown/blockquote.rb +51 -0
  34. data/lib/kramdown/parser/kramdown/codeblock.rb +63 -0
  35. data/lib/kramdown/parser/kramdown/codespan.rb +56 -0
  36. data/lib/kramdown/parser/kramdown/emphasis.rb +70 -0
  37. data/lib/kramdown/parser/kramdown/eob.rb +39 -0
  38. data/lib/kramdown/parser/kramdown/escaped_chars.rb +38 -0
  39. data/lib/kramdown/parser/kramdown/extensions.rb +204 -0
  40. data/lib/kramdown/parser/kramdown/footnote.rb +74 -0
  41. data/lib/kramdown/parser/kramdown/header.rb +68 -0
  42. data/lib/kramdown/parser/kramdown/horizontal_rule.rb +39 -0
  43. data/lib/kramdown/parser/kramdown/html.rb +169 -0
  44. data/lib/kramdown/parser/kramdown/html_entity.rb +44 -0
  45. data/lib/kramdown/parser/kramdown/image.rb +157 -0
  46. data/lib/kramdown/parser/kramdown/line_break.rb +38 -0
  47. data/lib/kramdown/parser/kramdown/link.rb +154 -0
  48. data/lib/kramdown/parser/kramdown/list.rb +240 -0
  49. data/lib/kramdown/parser/kramdown/math.rb +65 -0
  50. data/lib/kramdown/parser/kramdown/paragraph.rb +63 -0
  51. data/lib/kramdown/parser/kramdown/smart_quotes.rb +214 -0
  52. data/lib/kramdown/parser/kramdown/table.rb +178 -0
  53. data/lib/kramdown/parser/kramdown/typographic_symbol.rb +52 -0
  54. data/lib/kramdown/parser/markdown.rb +69 -0
  55. data/lib/kramdown/utils.rb +42 -0
  56. data/lib/kramdown/utils/entities.rb +348 -0
  57. data/lib/kramdown/utils/html.rb +85 -0
  58. data/lib/kramdown/utils/ordered_hash.rb +100 -0
  59. data/lib/kramdown/version.rb +28 -0
  60. metadata +140 -0
@@ -0,0 +1,339 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #--
4
+ # Copyright (C) 2009-2012 Thomas Leitner <t_leitner@gmx.at>
5
+ #
6
+ # This file is part of kramdown.
7
+ #
8
+ # kramdown is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ #++
21
+ #
22
+
23
+ require 'strscan'
24
+ require 'stringio'
25
+
26
+ #TODO: use [[:alpha:]] in all regexp to allow parsing of international values in 1.9.1
27
+ #NOTE: use @src.pre_match only before other check/match?/... operations, otherwise the content is changed
28
+
29
+ module Kramdown
30
+
31
+ module Parser
32
+
33
+ # Used for parsing a document in kramdown format.
34
+ #
35
+ # If you want to extend the functionality of the parser, you need to do the following:
36
+ #
37
+ # * Create a new subclass
38
+ # * add the needed parser methods
39
+ # * modify the @block_parsers and @span_parsers variables and add the names of your parser
40
+ # methods
41
+ #
42
+ # Here is a small example for an extended parser class that parses ERB style tags as raw text if
43
+ # they are used as span-level elements (an equivalent block-level parser should probably also be
44
+ # made to handle the block case):
45
+ #
46
+ # require 'kramdown/parser/kramdown'
47
+ #
48
+ # class Kramdown::Parser::ERBKramdown < Kramdown::Parser::Kramdown
49
+ #
50
+ # def initialize(source, options)
51
+ # super
52
+ # @span_parsers.unshift(:erb_tags)
53
+ # end
54
+ #
55
+ # ERB_TAGS_START = /<%.*?%>/
56
+ #
57
+ # def parse_erb_tags
58
+ # @src.pos += @src.matched_size
59
+ # @tree.children << Element.new(:raw, @src.matched)
60
+ # end
61
+ # define_parser(:erb_tags, ERB_TAGS_START, '<%')
62
+ #
63
+ # end
64
+ #
65
+ # The new parser can be used like this:
66
+ #
67
+ # require 'kramdown/document'
68
+ # # require the file with the above parser class
69
+ #
70
+ # Kramdown::Document.new(input_text, :input => 'ERBKramdown').to_html
71
+ #
72
+ class Kramdown < Base
73
+
74
+ include ::Kramdown
75
+
76
+ # Create a new Kramdown parser object with the given +options+.
77
+ def initialize(source, options)
78
+ super
79
+
80
+ reset_env
81
+
82
+ @root.options[:abbrev_defs] = {}
83
+ @alds = {}
84
+ @link_defs = {}
85
+ @footnotes = {}
86
+
87
+ @block_parsers = [:blank_line, :codeblock, :codeblock_fenced, :blockquote, :atx_header,
88
+ :horizontal_rule, :list, :definition_list, :block_html, :setext_header,
89
+ :table, :footnote_definition, :link_definition, :abbrev_definition,
90
+ :block_extensions, :block_math, :eob_marker, :paragraph]
91
+ @span_parsers = [:emphasis, :codespan, :autolink, :span_html, :footnote_marker, :link, :image, :smart_quotes, :inline_math,
92
+ :span_extensions, :html_entity, :typographic_syms, :line_break, :escaped_chars]
93
+
94
+ end
95
+ private_class_method(:new, :allocate)
96
+
97
+
98
+ # The source string provided on initialization is parsed into the @root element.
99
+ def parse
100
+ configure_parser
101
+ parse_blocks(@root, adapt_source(source))
102
+ update_tree(@root)
103
+ replace_abbreviations(@root)
104
+ @footnotes.each {|name,data| update_tree(data[:marker].value) if data[:marker]}
105
+ end
106
+
107
+ #######
108
+ protected
109
+ #######
110
+
111
+ # Adapt the object to allow parsing like specified in the options.
112
+ def configure_parser
113
+ @parsers = {}
114
+ (@block_parsers + @span_parsers).each do |name|
115
+ if self.class.has_parser?(name)
116
+ @parsers[name] = self.class.parser(name)
117
+ else
118
+ raise Kramdown::Error, "Unknown parser: #{name}"
119
+ end
120
+ end
121
+ @span_start, @span_start_re = span_parser_regexps
122
+ end
123
+
124
+ # Create the needed span parser regexps.
125
+ def span_parser_regexps(parsers = @span_parsers)
126
+ span_start = /#{parsers.map {|name| @parsers[name].span_start}.join('|')}/
127
+ [span_start, /(?=#{span_start})/]
128
+ end
129
+
130
+ # Parse all block-level elements in +text+ into the element +el+.
131
+ def parse_blocks(el, text = nil)
132
+ @stack.push([@tree, @src, @block_ial])
133
+ @tree, @src, @block_ial = el, (text.nil? ? @src : StringScanner.new(text)), nil
134
+
135
+ status = catch(:stop_block_parsing) do
136
+ while !@src.eos?
137
+ block_ial_set = @block_ial
138
+ @block_parsers.any? do |name|
139
+ if @src.check(@parsers[name].start_re)
140
+ send(@parsers[name].method)
141
+ else
142
+ false
143
+ end
144
+ end || begin
145
+ warning('Warning: this should not occur - no block parser handled the line')
146
+ add_text(@src.scan(/.*\n/))
147
+ end
148
+ @block_ial = nil if block_ial_set
149
+ end
150
+ end
151
+
152
+ @tree, @src, @block_ial = *@stack.pop
153
+ status
154
+ end
155
+
156
+ # Update the tree by parsing all :+raw_text+ elements with the span-level parser (resets the
157
+ # environment) and by updating the attributes from the IALs.
158
+ def update_tree(element)
159
+ last_blank = nil
160
+ element.children.map! do |child|
161
+ if child.type == :raw_text
162
+ last_blank = nil
163
+ reset_env(:src => StringScanner.new(child.value), :text_type => :text)
164
+ parse_spans(child)
165
+ child.children
166
+ elsif child.type == :eob
167
+ []
168
+ elsif child.type == :blank
169
+ if last_blank
170
+ last_blank.value << child.value
171
+ []
172
+ else
173
+ last_blank = child
174
+ child
175
+ end
176
+ else
177
+ last_blank = nil
178
+ update_tree(child)
179
+ update_attr_with_ial(child.attr, child.options[:ial]) if child.options[:ial]
180
+ child
181
+ end
182
+ end.flatten!
183
+ end
184
+
185
+ # Parse all span-level elements in the source string of @src into +el+.
186
+ #
187
+ # If the parameter +stop_re+ (a regexp) is used, parsing is immediately stopped if the regexp
188
+ # matches and if no block is given or if a block is given and it returns +true+.
189
+ #
190
+ # The parameter +parsers+ can be used to specify the (span-level) parsing methods that should
191
+ # be used for parsing.
192
+ #
193
+ # The parameter +text_type+ specifies the type which should be used for created text nodes.
194
+ def parse_spans(el, stop_re = nil, parsers = nil, text_type = @text_type)
195
+ @stack.push([@tree, @text_type]) unless @tree.nil?
196
+ @tree, @text_type = el, text_type
197
+
198
+ span_start = @span_start
199
+ span_start_re = @span_start_re
200
+ span_start, span_start_re = span_parser_regexps(parsers) if parsers
201
+ parsers = parsers || @span_parsers
202
+
203
+ used_re = (stop_re.nil? ? span_start_re : /(?=#{Regexp.union(stop_re, span_start)})/)
204
+ stop_re_found = false
205
+ while !@src.eos? && !stop_re_found
206
+ if result = @src.scan_until(used_re)
207
+ add_text(result)
208
+ if stop_re && @src.check(stop_re)
209
+ stop_re_found = (block_given? ? yield : true)
210
+ end
211
+ processed = parsers.any? do |name|
212
+ if @src.check(@parsers[name].start_re)
213
+ send(@parsers[name].method)
214
+ true
215
+ else
216
+ false
217
+ end
218
+ end unless stop_re_found
219
+ add_text(@src.getch) if !processed && !stop_re_found
220
+ else
221
+ (add_text(@src.rest); @src.terminate) unless stop_re
222
+ break
223
+ end
224
+ end
225
+
226
+ @tree, @text_type = @stack.pop
227
+
228
+ stop_re_found
229
+ end
230
+
231
+ # Reset the current parsing environment. The parameter +env+ can be used to set initial
232
+ # values for one or more environment variables.
233
+ def reset_env(opts = {})
234
+ opts = {:text_type => :raw_text, :stack => []}.merge(opts)
235
+ @src = opts[:src]
236
+ @tree = opts[:tree]
237
+ @block_ial = opts[:block_ial]
238
+ @stack = opts[:stack]
239
+ @text_type = opts[:text_type]
240
+ end
241
+
242
+ # Return the current parsing environment.
243
+ def save_env
244
+ [@src, @tree, @block_ial, @stack, @text_type]
245
+ end
246
+
247
+ # Restore the current parsing environment.
248
+ def restore_env(env)
249
+ @src, @tree, @block_ial, @stack, @text_type = *env
250
+ end
251
+
252
+ # Update the given attributes hash +attr+ with the information from the inline attribute list
253
+ # +ial+ and all referenced ALDs.
254
+ def update_attr_with_ial(attr, ial)
255
+ ial[:refs].each do |ref|
256
+ update_attr_with_ial(attr, ref) if ref = @alds[ref]
257
+ end if ial[:refs]
258
+ ial.each do |k,v|
259
+ if k == IAL_CLASS_ATTR
260
+ attr[k] = (attr[k] || '') << " #{v}"
261
+ attr[k].lstrip!
262
+ elsif k.kind_of?(String)
263
+ attr[k] = v
264
+ end
265
+ end
266
+ end
267
+
268
+ # Create a new block-level element, taking care of applying a preceding block IAL if it
269
+ # exists. This method should always be used for creating a block-level element!
270
+ def new_block_el(*args)
271
+ el = Element.new(*args)
272
+ el.options[:ial] = @block_ial if @block_ial && el.type != :blank && el.type != :eob
273
+ el
274
+ end
275
+
276
+ @@parsers = {}
277
+
278
+ # Struct class holding all the needed data for one block/span-level parser method.
279
+ Data = Struct.new(:name, :start_re, :span_start, :method)
280
+
281
+ # Add a parser method
282
+ #
283
+ # * with the given +name+,
284
+ # * using +start_re+ as start regexp
285
+ # * and, for span parsers, +span_start+ as a String that can be used in a regexp and
286
+ # which identifies the starting character(s)
287
+ #
288
+ # to the registry. The method name is automatically derived from the +name+ or can explicitly
289
+ # be set by using the +meth_name+ parameter.
290
+ def self.define_parser(name, start_re, span_start = nil, meth_name = "parse_#{name}")
291
+ raise "A parser with the name #{name} already exists!" if @@parsers.has_key?(name)
292
+ @@parsers[name] = Data.new(name, start_re, span_start, meth_name)
293
+ end
294
+
295
+ # Return the Data structure for the parser +name+.
296
+ def self.parser(name = nil)
297
+ @@parsers[name]
298
+ end
299
+
300
+ # Return +true+ if there is a parser called +name+.
301
+ def self.has_parser?(name)
302
+ @@parsers.has_key?(name)
303
+ end
304
+
305
+ # Regexp for matching indentation (one tab or four spaces)
306
+ INDENT = /^(?:\t| {4})/
307
+ # Regexp for matching the optional space (zero or up to three spaces)
308
+ OPT_SPACE = / {0,3}/
309
+
310
+ require 'kramdown/parser/kramdown/blank_line'
311
+ require 'kramdown/parser/kramdown/eob'
312
+ require 'kramdown/parser/kramdown/paragraph'
313
+ require 'kramdown/parser/kramdown/header'
314
+ require 'kramdown/parser/kramdown/blockquote'
315
+ require 'kramdown/parser/kramdown/table'
316
+ require 'kramdown/parser/kramdown/codeblock'
317
+ require 'kramdown/parser/kramdown/horizontal_rule'
318
+ require 'kramdown/parser/kramdown/list'
319
+ require 'kramdown/parser/kramdown/link'
320
+ require 'kramdown/parser/kramdown/image'
321
+ require 'kramdown/parser/kramdown/extensions'
322
+ require 'kramdown/parser/kramdown/footnote'
323
+ require 'kramdown/parser/kramdown/html'
324
+ require 'kramdown/parser/kramdown/escaped_chars'
325
+ require 'kramdown/parser/kramdown/html_entity'
326
+ require 'kramdown/parser/kramdown/line_break'
327
+ require 'kramdown/parser/kramdown/typographic_symbol'
328
+ require 'kramdown/parser/kramdown/autolink'
329
+ require 'kramdown/parser/kramdown/codespan'
330
+ require 'kramdown/parser/kramdown/emphasis'
331
+ require 'kramdown/parser/kramdown/smart_quotes'
332
+ require 'kramdown/parser/kramdown/math'
333
+ require 'kramdown/parser/kramdown/abbreviation'
334
+
335
+ end
336
+
337
+ end
338
+
339
+ end
@@ -0,0 +1,71 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #--
4
+ # Copyright (C) 2009-2012 Thomas Leitner <t_leitner@gmx.at>
5
+ #
6
+ # This file is part of kramdown.
7
+ #
8
+ # kramdown is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ #++
21
+ #
22
+
23
+ module Kramdown
24
+ module Parser
25
+ class Kramdown
26
+
27
+ ABBREV_DEFINITION_START = /^#{OPT_SPACE}\*\[(.+?)\]:(.*?)\n/
28
+
29
+ # Parse the link definition at the current location.
30
+ def parse_abbrev_definition
31
+ @src.pos += @src.matched_size
32
+ abbrev_id, abbrev_text = @src[1], @src[2]
33
+ abbrev_text.strip!
34
+ warning("Duplicate abbreviation ID '#{abbrev_id}' - overwriting") if @root.options[:abbrev_defs][abbrev_id]
35
+ @root.options[:abbrev_defs][abbrev_id] = abbrev_text
36
+ @tree.children << Element.new(:eob, :abbrev_def)
37
+ true
38
+ end
39
+ define_parser(:abbrev_definition, ABBREV_DEFINITION_START)
40
+
41
+ # Replace the abbreviation text with elements.
42
+ def replace_abbreviations(el, regexps = nil)
43
+ return if @root.options[:abbrev_defs].empty?
44
+ if !regexps
45
+ regexps = [Regexp.union(*@root.options[:abbrev_defs].keys.map {|k| /#{Regexp.escape(k)}/})]
46
+ regexps << /(?=(?:\W|^)#{regexps.first}(?!\w))/ # regexp should only match on word boundaries
47
+ end
48
+ el.children.map! do |child|
49
+ if child.type == :text
50
+ if child.value =~ regexps.first
51
+ result = []
52
+ strscan = StringScanner.new(child.value)
53
+ while temp = strscan.scan_until(regexps.last)
54
+ temp << strscan.scan(/\W|^/)
55
+ abbr = strscan.scan(regexps.first)
56
+ result << Element.new(:text, temp) << Element.new(:abbreviation, abbr)
57
+ end
58
+ result << Element.new(:text, strscan.rest)
59
+ else
60
+ child
61
+ end
62
+ else
63
+ replace_abbreviations(child, regexps)
64
+ child
65
+ end
66
+ end.flatten!
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,53 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #--
4
+ # Copyright (C) 2009-2012 Thomas Leitner <t_leitner@gmx.at>
5
+ #
6
+ # This file is part of kramdown.
7
+ #
8
+ # kramdown is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ #++
21
+ #
22
+
23
+ module Kramdown
24
+ module Parser
25
+ class Kramdown
26
+
27
+ if RUBY_VERSION == '1.8.5'
28
+ ACHARS = '\w\x80-\xFF'
29
+ elsif RUBY_VERSION < '1.9.0'
30
+ ACHARS = '\w'
31
+ else
32
+ ACHARS = '[[:alnum:]]'
33
+ end
34
+ AUTOLINK_START_STR = "<((mailto|https?|ftps?):.+?|[-.#{ACHARS}]+@[-#{ACHARS}]+(?:\.[-#{ACHARS}]+)*\.[a-z]+)>"
35
+ if RUBY_VERSION < '1.9.0'
36
+ AUTOLINK_START = /#{AUTOLINK_START_STR}/u
37
+ else
38
+ AUTOLINK_START = /#{AUTOLINK_START_STR}/
39
+ end
40
+
41
+ # Parse the autolink at the current location.
42
+ def parse_autolink
43
+ @src.pos += @src.matched_size
44
+ href = (@src[2].nil? ? "mailto:#{@src[1]}" : @src[1])
45
+ el = Element.new(:a, nil, {'href' => href})
46
+ add_text(@src[1].sub(/^mailto:/, ''), el)
47
+ @tree.children << el
48
+ end
49
+ define_parser(:autolink, AUTOLINK_START, '<')
50
+
51
+ end
52
+ end
53
+ end