-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
commonmarker.rb
executable file
·64 lines (58 loc) · 1.85 KB
/
commonmarker.rb
1
2
3
4
5
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
#!/usr/bin/env ruby
require 'commonmarker/commonmarker'
require 'commonmarker/config'
require 'commonmarker/renderer'
require 'commonmarker/renderer/html_renderer'
require 'commonmarker/version'
begin
require 'awesome_print'
rescue LoadError; end
module CommonMarker
# Public: Parses a Markdown string into an HTML string.
#
# text - A {String} of text
# option - Either a {Symbol} or {Array of Symbol}s indicating the parse options
#
# Returns a {String} of converted HTML.
def self.render_html(text, option = :default)
fail TypeError, 'text must be a string!' unless text.is_a?(String)
Node.markdown_to_html(text.encode('UTF-8'), Config.process_options(option)).force_encoding('UTF-8')
end
# Public: Parses a Markdown string into a `document` node.
#
# string - {String} to be parsed
# option - A {Symbol} or {Array of Symbol}s indicating the parse options
#
# Returns the `document` node.
def self.render_doc(text, option = :default)
fail TypeError, 'text must be a string!' unless text.is_a?(String)
text = text.encode('UTF-8')
Node.parse_document(text, text.bytesize, Config.process_options(option))
end
class Node
# Public: An iterator that "walks the tree," descending into children recursively.
#
# blk - A {Proc} representing the action to take for each child
def walk(&blk)
yield self
each_child do |child|
child.walk(&blk)
end
end
# Public: Convert the node to an HTML string.
#
# Returns a {String}.
def to_html(option = :default)
_render_html(Config.process_options(option)).force_encoding('utf-8')
end
# Internal: Iterate over the children (if any) of the current pointer.
def each_child
child = first_child
while child
nextchild = child.next
yield child
child = nextchild
end
end
end
end