motion-kramdown 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +84 -0
  3. data/lib/kramdown/compatibility.rb +36 -0
  4. data/lib/kramdown/converter/base.rb +259 -0
  5. data/lib/kramdown/converter/html.rb +461 -0
  6. data/lib/kramdown/converter/kramdown.rb +423 -0
  7. data/lib/kramdown/converter/latex.rb +600 -0
  8. data/lib/kramdown/converter/math_engine/itex2mml.rb +39 -0
  9. data/lib/kramdown/converter/math_engine/mathjax.rb +33 -0
  10. data/lib/kramdown/converter/math_engine/ritex.rb +38 -0
  11. data/lib/kramdown/converter/pdf.rb +624 -0
  12. data/lib/kramdown/converter/remove_html_tags.rb +53 -0
  13. data/lib/kramdown/converter/syntax_highlighter/coderay.rb +78 -0
  14. data/lib/kramdown/converter/syntax_highlighter/rouge.rb +37 -0
  15. data/lib/kramdown/converter/toc.rb +69 -0
  16. data/lib/kramdown/converter.rb +69 -0
  17. data/lib/kramdown/document.rb +144 -0
  18. data/lib/kramdown/element.rb +515 -0
  19. data/lib/kramdown/error.rb +17 -0
  20. data/lib/kramdown/options.rb +584 -0
  21. data/lib/kramdown/parser/base.rb +130 -0
  22. data/lib/kramdown/parser/gfm.rb +55 -0
  23. data/lib/kramdown/parser/html.rb +575 -0
  24. data/lib/kramdown/parser/kramdown/abbreviation.rb +67 -0
  25. data/lib/kramdown/parser/kramdown/autolink.rb +37 -0
  26. data/lib/kramdown/parser/kramdown/blank_line.rb +30 -0
  27. data/lib/kramdown/parser/kramdown/block_boundary.rb +33 -0
  28. data/lib/kramdown/parser/kramdown/blockquote.rb +39 -0
  29. data/lib/kramdown/parser/kramdown/codeblock.rb +56 -0
  30. data/lib/kramdown/parser/kramdown/codespan.rb +44 -0
  31. data/lib/kramdown/parser/kramdown/emphasis.rb +61 -0
  32. data/lib/kramdown/parser/kramdown/eob.rb +26 -0
  33. data/lib/kramdown/parser/kramdown/escaped_chars.rb +25 -0
  34. data/lib/kramdown/parser/kramdown/extensions.rb +201 -0
  35. data/lib/kramdown/parser/kramdown/footnote.rb +56 -0
  36. data/lib/kramdown/parser/kramdown/header.rb +59 -0
  37. data/lib/kramdown/parser/kramdown/horizontal_rule.rb +27 -0
  38. data/lib/kramdown/parser/kramdown/html.rb +160 -0
  39. data/lib/kramdown/parser/kramdown/html_entity.rb +33 -0
  40. data/lib/kramdown/parser/kramdown/line_break.rb +25 -0
  41. data/lib/kramdown/parser/kramdown/link.rb +139 -0
  42. data/lib/kramdown/parser/kramdown/list.rb +256 -0
  43. data/lib/kramdown/parser/kramdown/math.rb +54 -0
  44. data/lib/kramdown/parser/kramdown/paragraph.rb +54 -0
  45. data/lib/kramdown/parser/kramdown/smart_quotes.rb +174 -0
  46. data/lib/kramdown/parser/kramdown/table.rb +171 -0
  47. data/lib/kramdown/parser/kramdown/typographic_symbol.rb +44 -0
  48. data/lib/kramdown/parser/kramdown.rb +359 -0
  49. data/lib/kramdown/parser/markdown.rb +56 -0
  50. data/lib/kramdown/parser.rb +27 -0
  51. data/lib/kramdown/utils/configurable.rb +44 -0
  52. data/lib/kramdown/utils/entities.rb +347 -0
  53. data/lib/kramdown/utils/html.rb +75 -0
  54. data/lib/kramdown/utils/ordered_hash.rb +87 -0
  55. data/lib/kramdown/utils/string_scanner.rb +74 -0
  56. data/lib/kramdown/utils/unidecoder.rb +51 -0
  57. data/lib/kramdown/utils.rb +58 -0
  58. data/lib/kramdown/version.rb +15 -0
  59. data/lib/kramdown.rb +10 -0
  60. data/lib/motion-kramdown.rb +47 -0
  61. data/lib/rubymotion/encodings.rb +37 -0
  62. data/lib/rubymotion/rexml_shim.rb +25 -0
  63. data/lib/rubymotion/set.rb +1349 -0
  64. data/lib/rubymotion/version.rb +6 -0
  65. data/spec/document_tree.rb +48 -0
  66. data/spec/gfm_to_html.rb +95 -0
  67. data/spec/helpers/it_behaves_like.rb +27 -0
  68. data/spec/helpers/option_file.rb +46 -0
  69. data/spec/helpers/spec_options.rb +37 -0
  70. data/spec/helpers/tidy.rb +12 -0
  71. data/spec/html_to_html.rb +40 -0
  72. data/spec/html_to_kramdown_to_html.rb +46 -0
  73. data/spec/kramdown_to_xxx.rb +40 -0
  74. data/spec/test_location.rb +203 -0
  75. data/spec/test_string_scanner_kramdown.rb +19 -0
  76. data/spec/text_to_kramdown_to_html.rb +52 -0
  77. data/spec/text_to_latex.rb +33 -0
  78. metadata +164 -0
@@ -0,0 +1,584 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #--
4
+ # Copyright (C) 2009-2014 Thomas Leitner <t_leitner@gmx.at>
5
+ #
6
+ # This file is part of kramdown which is licensed under the MIT.
7
+ #++
8
+ #
9
+
10
+ # RM equire 'yaml'
11
+
12
+ module Kramdown
13
+
14
+ # This module defines all options that are used by parsers and/or converters as well as providing
15
+ # methods to deal with the options.
16
+ module Options
17
+
18
+ # Helper class introducing a boolean type for specifying boolean values (+true+ and +false+) as
19
+ # option types.
20
+ class Boolean
21
+
22
+ # Return +true+ if +other+ is either +true+ or +false+
23
+ def self.===(other)
24
+ FalseClass === other || TrueClass === other
25
+ end
26
+
27
+ end
28
+
29
+ # ----------------------------
30
+ # :section: Option definitions
31
+ #
32
+ # This sections describes the methods that can be used on the Options module.
33
+ # ----------------------------
34
+
35
+ # Struct class for storing the definition of an option.
36
+ Definition = Struct.new(:name, :type, :default, :desc, :validator)
37
+
38
+ # Allowed option types.
39
+ ALLOWED_TYPES = [String, Integer, Float, Symbol, Boolean, Object]
40
+
41
+ @options = {}
42
+
43
+ # Define a new option called +name+ (a Symbol) with the given +type+ (String, Integer, Float,
44
+ # Symbol, Boolean, Object), default value +default+ and the description +desc+. If a block is
45
+ # specified, it should validate the value and either raise an error or return a valid value.
46
+ #
47
+ # The type 'Object' should only be used for complex types for which none of the other types
48
+ # suffices. A block needs to be specified when using type 'Object' and it has to cope with
49
+ # a value given as string and as the opaque type.
50
+ def self.define(name, type, default, desc, &block)
51
+ name = name.to_sym
52
+ raise ArgumentError, "Option name #{name} is already used" if @options.has_key?(name)
53
+ raise ArgumentError, "Invalid option type #{type} specified" if !ALLOWED_TYPES.include?(type)
54
+ raise ArgumentError, "Invalid type for default value" if !(type === default) && !default.nil?
55
+ raise ArgumentError, "Missing validator block" if type == Object && block.nil?
56
+ @options[name] = Definition.new(name, type, default, desc, block)
57
+ end
58
+
59
+ # Return all option definitions.
60
+ def self.definitions
61
+ @options
62
+ end
63
+
64
+ # Return +true+ if an option called +name+ is defined.
65
+ def self.defined?(name)
66
+ @options.has_key?(name.to_sym)
67
+ end
68
+
69
+ # Return a Hash with the default values for all options.
70
+ def self.defaults
71
+ temp = {}
72
+ @options.each {|n, o| temp[o.name] = o.default}
73
+ temp
74
+ end
75
+
76
+ # Merge the #defaults Hash with the *parsed* options from the given Hash, i.e. only valid option
77
+ # names are considered and their value is run through the #parse method.
78
+ def self.merge(hash)
79
+ temp = defaults
80
+ hash.each do |k,v|
81
+ k = k.to_sym
82
+ @options.has_key?(k) ? temp[k] = parse(k, v) : temp[k] = v
83
+ end
84
+ temp
85
+ end
86
+
87
+ # Parse the given value +data+ as if it was a value for the option +name+ and return the parsed
88
+ # value with the correct type.
89
+ #
90
+ # If +data+ already has the correct type, it is just returned. Otherwise it is converted to a
91
+ # String and then to the correct type.
92
+ def self.parse(name, data)
93
+ name = name.to_sym
94
+ raise ArgumentError, "No option named #{name} defined" if !@options.has_key?(name)
95
+ if !(@options[name].type === data)
96
+ data = data.to_s
97
+ data = if @options[name].type == String
98
+ data
99
+ elsif @options[name].type == Integer
100
+ Integer(data) rescue raise Kramdown::Error, "Invalid integer value for option '#{name}': '#{data}'"
101
+ elsif @options[name].type == Float
102
+ Float(data) rescue raise Kramdown::Error, "Invalid float value for option '#{name}': '#{data}'"
103
+ elsif @options[name].type == Symbol
104
+ str_to_sym(data)
105
+ elsif @options[name].type == Boolean
106
+ data.downcase.strip != 'false' && !data.empty?
107
+ end
108
+ end
109
+ data = @options[name].validator[data] if @options[name].validator
110
+ data
111
+ end
112
+
113
+ # Converts the given String +data+ into a Symbol or +nil+ with the
114
+ # following provisions:
115
+ #
116
+ # - A leading colon is stripped from the string.
117
+ # - An empty value or a value equal to "nil" results in +nil+.
118
+ def self.str_to_sym(data)
119
+ data = data.strip
120
+ data = data[1..-1] if data[0] == ?:
121
+ (data.empty? || data == 'nil' ? nil : data.to_sym)
122
+ end
123
+
124
+ # ----------------------------
125
+ # :section: Option Validators
126
+ #
127
+ # This sections contains all pre-defined option validators.
128
+ # ----------------------------
129
+
130
+ # Ensures that the option value +val+ for the option called +name+ is a valid array. The
131
+ # parameter +val+ can be
132
+ #
133
+ # - a comma separated string which is split into an array of values
134
+ # - or an array.
135
+ #
136
+ # Additionally, the array is checked for the correct size.
137
+ def self.simple_array_validator(val, name, size)
138
+ if String === val
139
+ val = val.split(/,/)
140
+ elsif !(Array === val)
141
+ raise Kramdown::Error, "Invalid type #{val.class} for option #{name}"
142
+ end
143
+ if val.size != size
144
+ raise Kramdown::Error, "Option #{name} needs exactly #{size} values"
145
+ end
146
+ val
147
+ end
148
+
149
+ # Ensures that the option value +val+ for the option called +name+ is a valid hash. The
150
+ # parameter +val+ can be
151
+ #
152
+ # - a hash in YAML format
153
+ # - or a Ruby Hash object.
154
+ def self.simple_hash_validator(val, name)
155
+ if String === val
156
+ begin
157
+ val = YAML.load(val)
158
+ rescue RuntimeError, ArgumentError, SyntaxError
159
+ raise Kramdown::Error, "Invalid YAML value for option #{name}"
160
+ end
161
+ end
162
+ raise Kramdown::Error, "Invalid type #{val.class} for option #{name}" if !(Hash === val)
163
+ val
164
+ end
165
+
166
+ # ----------------------------
167
+ # :section: Option Definitions
168
+ #
169
+ # This sections contains all option definitions that are used by the included
170
+ # parsers/converters.
171
+ # ----------------------------
172
+
173
+ define(:template, String, '', <<EOF)
174
+ The name of an ERB template file that should be used to wrap the output
175
+ or the ERB template itself.
176
+
177
+ This is used to wrap the output in an environment so that the output can
178
+ be used as a stand-alone document. For example, an HTML template would
179
+ provide the needed header and body tags so that the whole output is a
180
+ valid HTML file. If no template is specified, the output will be just
181
+ the converted text.
182
+
183
+ When resolving the template file, the given template name is used first.
184
+ If such a file is not found, the converter extension (the same as the
185
+ converter name) is appended. If the file still cannot be found, the
186
+ templates name is interpreted as a template name that is provided by
187
+ kramdown (without the converter extension). If the file is still not
188
+ found, the template name is checked if it starts with 'string://' and if
189
+ it does, this prefix is removed and the rest is used as template
190
+ content.
191
+
192
+ kramdown provides a default template named 'document' for each converter.
193
+
194
+ Default: ''
195
+ Used by: all converters
196
+ EOF
197
+
198
+ define(:auto_ids, Boolean, true, <<EOF)
199
+ Use automatic header ID generation
200
+
201
+ If this option is `true`, ID values for all headers are automatically
202
+ generated if no ID is explicitly specified.
203
+
204
+ Default: true
205
+ Used by: HTML/Latex converter
206
+ EOF
207
+
208
+ define(:auto_id_stripping, Boolean, false, <<EOF)
209
+ Strip all formatting from header text for automatic ID generation
210
+
211
+ If this option is `true`, only the text elements of a header are used
212
+ for generating the ID later (in contrast to just using the raw header
213
+ text line).
214
+
215
+ This option will be removed in version 2.0 because this will be the
216
+ default then.
217
+
218
+ Default: false
219
+ Used by: kramdown parser
220
+ EOF
221
+
222
+ define(:auto_id_prefix, String, '', <<EOF)
223
+ Prefix used for automatically generated header IDs
224
+
225
+ This option can be used to set a prefix for the automatically generated
226
+ header IDs so that there is no conflict when rendering multiple kramdown
227
+ documents into one output file separately. The prefix should only
228
+ contain characters that are valid in an ID!
229
+
230
+ Default: ''
231
+ Used by: HTML/Latex converter
232
+ EOF
233
+
234
+ define(:transliterated_header_ids, Boolean, false, <<EOF)
235
+ Transliterate the header text before generating the ID
236
+
237
+ Only ASCII characters are used in headers IDs. This is not good for
238
+ languages with many non-ASCII characters. By enabling this option
239
+ the header text is transliterated to ASCII as good as possible so that
240
+ the resulting header ID is more useful.
241
+
242
+ The stringex library needs to be installed for this feature to work!
243
+
244
+ Default: false
245
+ Used by: HTML/Latex converter
246
+ EOF
247
+
248
+ define(:parse_block_html, Boolean, false, <<EOF)
249
+ Process kramdown syntax in block HTML tags
250
+
251
+ If this option is `true`, the kramdown parser processes the content of
252
+ block HTML tags as text containing block-level elements. Since this is
253
+ not wanted normally, the default is `false`. It is normally better to
254
+ selectively enable kramdown processing via the markdown attribute.
255
+
256
+ Default: false
257
+ Used by: kramdown parser
258
+ EOF
259
+
260
+ define(:parse_span_html, Boolean, true, <<EOF)
261
+ Process kramdown syntax in span HTML tags
262
+
263
+ If this option is `true`, the kramdown parser processes the content of
264
+ span HTML tags as text containing span-level elements.
265
+
266
+ Default: true
267
+ Used by: kramdown parser
268
+ EOF
269
+
270
+ define(:html_to_native, Boolean, false, <<EOF)
271
+ Convert HTML elements to native elements
272
+
273
+ If this option is `true`, the parser converts HTML elements to native
274
+ elements. For example, when parsing `<em>hallo</em>` the emphasis tag
275
+ would normally be converted to an `:html` element with tag type `:em`.
276
+ If `html_to_native` is `true`, then the emphasis would be converted to a
277
+ native `:em` element.
278
+
279
+ This is useful for converters that cannot deal with HTML elements.
280
+
281
+ Default: false
282
+ Used by: kramdown parser
283
+ EOF
284
+
285
+ define(:link_defs, Object, {}, <<EOF) do |val|
286
+ Pre-defines link definitions
287
+
288
+ This option can be used to pre-define link definitions. The value needs
289
+ to be a Hash where the keys are the link identifiers and the values are
290
+ two element Arrays with the link URL and the link title.
291
+
292
+ If the value is a String, it has to contain a valid YAML hash and the
293
+ hash has to follow the above guidelines.
294
+
295
+ Default: {}
296
+ Used by: kramdown parser
297
+ EOF
298
+ val = simple_hash_validator(val, :link_defs)
299
+ val.each do |k,v|
300
+ if !(Array === v) || v.size > 2 || v.size < 1
301
+ raise Kramdown::Error, "Invalid structure for hash value of option #{name}"
302
+ end
303
+ v << nil if v.size == 1
304
+ end
305
+ val
306
+ end
307
+
308
+ define(:footnote_nr, Integer, 1, <<EOF)
309
+ The number of the first footnote
310
+
311
+ This option can be used to specify the number that is used for the first
312
+ footnote.
313
+
314
+ Default: 1
315
+ Used by: HTML converter
316
+ EOF
317
+
318
+ define(:enable_coderay, Boolean, true, <<EOF)
319
+ Use coderay for syntax highlighting
320
+
321
+ If this option is `true`, coderay is used by the HTML converter for
322
+ syntax highlighting the content of code spans and code blocks.
323
+
324
+ Default: true
325
+ Used by: HTML converter
326
+ EOF
327
+
328
+ define(:coderay_wrap, Symbol, :div, <<EOF)
329
+ Defines how the highlighted code should be wrapped
330
+
331
+ The possible values are :span, :div or nil.
332
+
333
+ Default: :div
334
+ Used by: HTML converter
335
+ EOF
336
+
337
+ define(:coderay_line_numbers, Symbol, :inline, <<EOF)
338
+ Defines how and if line numbers should be shown
339
+
340
+ The possible values are :table, :inline or nil. If this option is
341
+ nil, no line numbers are shown.
342
+
343
+ Default: :inline
344
+ Used by: HTML converter
345
+ EOF
346
+
347
+ define(:coderay_line_number_start, Integer, 1, <<EOF)
348
+ The start value for the line numbers
349
+
350
+ Default: 1
351
+ Used by: HTML converter
352
+ EOF
353
+
354
+ define(:coderay_tab_width, Integer, 8, <<EOF)
355
+ The tab width used in highlighted code
356
+
357
+ Used by: HTML converter
358
+ EOF
359
+
360
+ define(:coderay_bold_every, Object, 10, <<EOF) do |val|
361
+ Defines how often a line number should be made bold
362
+
363
+ Can either be an integer or false (to turn off bold line numbers
364
+ completely).
365
+
366
+ Default: 10
367
+ Used by: HTML converter
368
+ EOF
369
+ if val == false || val.to_s == 'false'
370
+ false
371
+ else
372
+ Integer(val.to_s) rescue raise Kramdown::Error, "Invalid value for option 'coderay_bold_every'"
373
+ end
374
+ end
375
+
376
+ define(:coderay_css, Symbol, :style, <<EOF)
377
+ Defines how the highlighted code gets styled
378
+
379
+ Possible values are :class (CSS classes are applied to the code
380
+ elements, one must supply the needed CSS file) or :style (default CSS
381
+ styles are directly applied to the code elements).
382
+
383
+ Default: style
384
+ Used by: HTML converter
385
+ EOF
386
+
387
+ define(:coderay_default_lang, Symbol, nil, <<EOF)
388
+ Sets the default language for highlighting code blocks
389
+
390
+ If no language is set for a code block, the default language is used
391
+ instead. The value has to be one of the languages supported by coderay
392
+ or nil if no default language should be used.
393
+
394
+ Default: nil
395
+ Used by: HTML converter
396
+ EOF
397
+
398
+ define(:entity_output, Symbol, :as_char, <<EOF)
399
+ Defines how entities are output
400
+
401
+ The possible values are :as_input (entities are output in the same
402
+ form as found in the input), :numeric (entities are output in numeric
403
+ form), :symbolic (entities are output in symbolic form if possible) or
404
+ :as_char (entities are output as characters if possible, only available
405
+ on Ruby 1.9).
406
+
407
+ Default: :as_char
408
+ Used by: HTML converter, kramdown converter
409
+ EOF
410
+
411
+ define(:toc_levels, Object, (1..6).to_a, <<EOF) do |val|
412
+ Defines the levels that are used for the table of contents
413
+
414
+ The individual levels can be specified by separating them with commas
415
+ (e.g. 1,2,3) or by using the range syntax (e.g. 1..3). Only the
416
+ specified levels are used for the table of contents.
417
+
418
+ Default: 1..6
419
+ Used by: HTML/Latex converter
420
+ EOF
421
+ if String === val
422
+ if val =~ /^(\d)\.\.(\d)$/
423
+ val = Range.new($1.to_i, $2.to_i).to_a
424
+ elsif val =~ /^\d(?:,\d)*$/
425
+ val = val.split(/,/).map {|s| s.to_i}.uniq
426
+ else
427
+ raise Kramdown::Error, "Invalid syntax for option toc_levels"
428
+ end
429
+ elsif Array === val
430
+ val = val.map {|s| s.to_i}.uniq
431
+ else
432
+ raise Kramdown::Error, "Invalid type #{val.class} for option toc_levels"
433
+ end
434
+ if val.any? {|i| !(1..6).include?(i)}
435
+ raise Kramdown::Error, "Level numbers for option toc_levels have to be integers from 1 to 6"
436
+ end
437
+ val
438
+ end
439
+
440
+ define(:line_width, Integer, 72, <<EOF)
441
+ Defines the line width to be used when outputting a document
442
+
443
+ Default: 72
444
+ Used by: kramdown converter
445
+ EOF
446
+
447
+ define(:latex_headers, Object, %w{section subsection subsubsection paragraph subparagraph subparagraph}, <<EOF) do |val|
448
+ Defines the LaTeX commands for different header levels
449
+
450
+ The commands for the header levels one to six can be specified by
451
+ separating them with commas.
452
+
453
+ Default: section,subsection,subsubsection,paragraph,subparagraph,subparagraph
454
+ Used by: Latex converter
455
+ EOF
456
+ simple_array_validator(val, :latex_headers, 6)
457
+ end
458
+
459
+ define(:smart_quotes, Object, %w{lsquo rsquo ldquo rdquo}, <<EOF) do |val|
460
+ Defines the HTML entity names or code points for smart quote output
461
+
462
+ The entities identified by entity name or code point that should be
463
+ used for, in order, a left single quote, a right single quote, a left
464
+ double and a right double quote are specified by separating them with
465
+ commas.
466
+
467
+ Default: lsquo,rsquo,ldquo,rdquo
468
+ Used by: HTML/Latex converter
469
+ EOF
470
+ val = simple_array_validator(val, :smart_quotes, 4)
471
+ # RM val.map! {|v| Integer(v) rescue v} TODO
472
+ val
473
+ end
474
+
475
+ define(:remove_block_html_tags, Boolean, true, <<EOF)
476
+ Remove block HTML tags
477
+
478
+ If this option is `true`, the RemoveHtmlTags converter removes
479
+ block HTML tags.
480
+
481
+ Default: true
482
+ Used by: RemoveHtmlTags converter
483
+ EOF
484
+
485
+ define(:remove_span_html_tags, Boolean, false, <<EOF)
486
+ Remove span HTML tags
487
+
488
+ If this option is `true`, the RemoveHtmlTags converter removes
489
+ span HTML tags.
490
+
491
+ Default: false
492
+ Used by: RemoveHtmlTags converter
493
+ EOF
494
+
495
+ define(:header_offset, Integer, 0, <<EOF)
496
+ Sets the output offset for headers
497
+
498
+ If this option is c (may also be negative) then a header with level n
499
+ will be output as a header with level c+n. If c+n is lower than 1,
500
+ level 1 will be used. If c+n is greater than 6, level 6 will be used.
501
+
502
+ Default: 0
503
+ Used by: HTML converter, Kramdown converter, Latex converter
504
+ EOF
505
+
506
+ define(:hard_wrap, Boolean, true, <<EOF)
507
+ Interprets line breaks literally
508
+
509
+ Insert HTML `<br />` tags inside paragraphs where the original Markdown
510
+ document had newlines (by default, Markdown ignores these newlines).
511
+
512
+ Default: true
513
+ Used by: GFM parser
514
+ EOF
515
+
516
+ define(:syntax_highlighter, Symbol, :coderay, <<EOF)
517
+ Set the syntax highlighter
518
+
519
+ Specifies the syntax highlighter that should be used for highlighting
520
+ code blocks and spans. If this option is set to +nil+, no syntax
521
+ highlighting is done.
522
+
523
+ Options for the syntax highlighter can be set with the
524
+ syntax_highlighter_opts configuration option.
525
+
526
+ Default: coderay
527
+ Used by: HTML converter
528
+ EOF
529
+
530
+ define(:syntax_highlighter_opts, Object, {}, <<EOF) do |val|
531
+ Set the syntax highlighter options
532
+
533
+ Specifies options for the syntax highlighter set via the
534
+ syntax_highlighter configuration option.
535
+
536
+ The value needs to be a hash with key-value pairs that are understood by
537
+ the used syntax highlighter.
538
+
539
+ Default: {}
540
+ Used by: HTML converter
541
+ EOF
542
+ val = simple_hash_validator(val, :syntax_highlighter_opts)
543
+ val.keys.each do |k|
544
+ val[k.kind_of?(String) ? str_to_sym(k) : k] = val.delete(k)
545
+ end
546
+ val
547
+ end
548
+
549
+ define(:math_engine, Symbol, :mathjax, <<EOF)
550
+ Set the math engine
551
+
552
+ Specifies the math engine that should be used for converting math
553
+ blocks/spans. If this option is set to +nil+, no math engine is used and
554
+ the math blocks/spans are output as is.
555
+
556
+ Options for the selected math engine can be set with the
557
+ math_engine_opts configuration option.
558
+
559
+ Default: mathjax
560
+ Used by: HTML converter
561
+ EOF
562
+
563
+ define(:math_engine_opts, Object, {}, <<EOF) do |val|
564
+ Set the math engine options
565
+
566
+ Specifies options for the math engine set via the math_engine
567
+ configuration option.
568
+
569
+ The value needs to be a hash with key-value pairs that are understood by
570
+ the used math engine.
571
+
572
+ Default: {}
573
+ Used by: HTML converter
574
+ EOF
575
+ val = simple_hash_validator(val, :math_engine_opts)
576
+ val.keys.each do |k|
577
+ val[k.kind_of?(String) ? str_to_sym(k) : k] = val.delete(k)
578
+ end
579
+ val
580
+ end
581
+
582
+ end
583
+
584
+ end
@@ -0,0 +1,130 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #--
4
+ # Copyright (C) 2009-2014 Thomas Leitner <t_leitner@gmx.at>
5
+ #
6
+ # This file is part of kramdown which is licensed under the MIT.
7
+ #++
8
+ #
9
+
10
+ # RM require 'kramdown/utils/string_scanner'
11
+
12
+ module Kramdown
13
+
14
+ module Parser
15
+
16
+ # == \Base class for parsers
17
+ #
18
+ # This class serves as base class for parsers. It provides common methods that can/should be
19
+ # used by all parsers, especially by those using StringScanner(Kramdown) for parsing.
20
+ #
21
+ # A parser object is used as a throw-away object, i.e. it is only used for storing the needed
22
+ # state information during parsing. Therefore one can't instantiate a parser object directly but
23
+ # only use the Base::parse method.
24
+ #
25
+ # == Implementing a parser
26
+ #
27
+ # Implementing a new parser is rather easy: just derive a new class from this class and put it
28
+ # in the Kramdown::Parser module -- the latter is needed so that the auto-detection of the new
29
+ # parser works correctly. Then you need to implement the +#parse+ method which has to contain
30
+ # the parsing code.
31
+ #
32
+ # Have a look at the Base::parse, Base::new and Base#parse methods for additional information!
33
+ class Base
34
+
35
+ # The hash with the parsing options.
36
+ attr_reader :options
37
+
38
+ # The array with the parser warnings.
39
+ attr_reader :warnings
40
+
41
+ # The original source string.
42
+ attr_reader :source
43
+
44
+ # The root element of element tree that is created from the source string.
45
+ attr_reader :root
46
+
47
+ # Initialize the parser object with the +source+ string and the parsing +options+.
48
+ #
49
+ # The @root element, the @warnings array and @text_type (specifies the default type for newly
50
+ # created text nodes) are automatically initialized.
51
+ def initialize(source, options)
52
+ @source = source
53
+ @options = Kramdown::Options.merge(options)
54
+ @root = Element.new(:root, nil, nil, :encoding => (source.encoding rescue nil), :location => 1)
55
+ @warnings = []
56
+ @text_type = :text
57
+ end
58
+ private_class_method(:new, :allocate)
59
+
60
+ # Parse the +source+ string into an element tree, possibly using the parsing +options+, and
61
+ # return the root element of the element tree and an array with warning messages.
62
+ #
63
+ # Initializes a new instance of the calling class and then calls the +#parse+ method that must
64
+ # be implemented by each subclass.
65
+ def self.parse(source, options = {})
66
+ parser = new(source, options)
67
+ parser.parse
68
+ [parser.root, parser.warnings]
69
+ end
70
+
71
+ # Parse the source string into an element tree.
72
+ #
73
+ # The parsing code should parse the source provided in @source and build an element tree the
74
+ # root of which should be @root.
75
+ #
76
+ # This is the only method that has to be implemented by sub-classes!
77
+ def parse
78
+ raise NotImplementedError
79
+ end
80
+
81
+ # Add the given warning +text+ to the warning array.
82
+ def warning(text)
83
+ @warnings << text
84
+ #TODO: add position information
85
+ end
86
+
87
+ # Modify the string +source+ to be usable by the parser (unifies line ending characters to
88
+ # +\n+ and makes sure +source+ ends with a new line character).
89
+ def adapt_source(source)
90
+ if source.respond_to?(:encode)
91
+ raise "The encoding of the source text is not valid!" if !source.valid_encoding?
92
+ source = source.encode('UTF-8')
93
+ end
94
+ source.gsub(/\r\n?/, "\n").chomp + "\n"
95
+ end
96
+
97
+ # This helper method adds the given +text+ either to the last element in the +tree+ if it is a
98
+ # +type+ element or creates a new text element with the given +type+.
99
+ def add_text(text, tree = @tree, type = @text_type)
100
+ last = tree.children.last
101
+ if last && last.type == type
102
+ last.value << text
103
+ elsif !text.empty?
104
+ tree.children << Element.new(type, text, nil, :location => (last && last.options[:location] || tree.options[:location]))
105
+ end
106
+ end
107
+
108
+ # Extract the part of the StringScanner +strscan+ backed string specified by the +range+. This
109
+ # method works correctly under Ruby 1.8 and Ruby 1.9.
110
+ def extract_string(range, strscan)
111
+ result = nil
112
+ if strscan.string.respond_to?(:encoding)
113
+ begin
114
+ enc = strscan.string.encoding
115
+ strscan.string.force_encoding('ASCII-8BIT')
116
+ result = strscan.string[range].force_encoding(enc)
117
+ ensure
118
+ strscan.string.force_encoding(enc)
119
+ end
120
+ else
121
+ result = strscan.string[range]
122
+ end
123
+ result
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+
130
+ end