Module: ModelFormatting
- Defined in:
- lib/model_formatting.rb,
lib/model_formatting/instance_methods.rb
Defined Under Namespace
Modules: Init, InstanceMethods
Classes: CodePart, Config, FormattedPart, Part
Constant Summary
collapse
- CODEBLOCK_RE =
/^@@@( ([a-z]+)\s*)?$/
Class Method Summary
collapse
Class Method Details
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
|
# File 'lib/model_formatting.rb', line 142
def self.(text, *regexes)
= {}
regexes.each do |regex|
text.gsub!(regex) do |match|
md5 = Digest::MD5.hexdigest(match)
[md5] ||= []
[md5] << match
"{mkd-extraction-#{md5}}"
end
end
yield text
while !.keys.empty?
text.gsub!(/\{mkd-extraction-([0-9a-f]{32})\}/) do
value = [$1].pop
.delete($1) if [$1].empty?
value
end
end
text
end
|
173
174
175
176
177
|
# File 'lib/model_formatting.rb', line 173
def self.(text, *tag_names)
text, *tag_names.map { |n| tag_name_to_regex(n) } do |text|
yield text
end
end
|
.gfm(text) ⇒ Object
179
180
181
182
183
184
185
186
|
# File 'lib/model_formatting.rb', line 179
def self.gfm(text)
(text, :pre) do |txt|
text.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
end
end
end
|
.parse_text_parts(format, options, text) ⇒ Object
Parse a string into a given array of [CodeBlcok, FormattedBlock, CodeBlock, FormattedBlock]
@@@ my code block
@@@ end code block
Blah blah formatted block
@@@ my code block
@@@ end code block
Blah blah formatted block
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
|
# File 'lib/model_formatting.rb', line 113
def self.parse_text_parts(format, options, text)
parts = []
current_part = nil
in_code_block = false
text.split("\n").each do |line|
if line.rstrip =~ CODEBLOCK_RE
line.rstrip!
if in_code_block
parts << current_part
current_part = nil
else
if current_part then parts << current_part end
current_part = CodePart.new(format, $2)
end
in_code_block = !in_code_block
else
if !in_code_block && current_part.is_a?(CodePart)
parts << current_part
current_part = nil
end
current_part ||= FormattedPart.new(format, options)
current_part << line
end
end
parts << current_part if current_part
parts.compact!
parts.each { |p| p.compact! }
end
|
.process(format, text, options = {}) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/model_formatting.rb', line 75
def self.process(format, text, options = {})
parts = parse_text_parts(format, options, text)
string = parts.map { |p| p.formatted_string } * "\n"
string.gsub! /\r/, ''
if format == :html
string.gsub! /code><\/pre>/, "code>\n</pre>" string = gfm(string)
string = process_markdown(string)
string.gsub! /\s+<\/code>/, '</code>' end
if options[:after]
(string, :pre, :code) do |str|
str.replace options[:after].call(format, str, options)
end
end
if format == :html
if options[:white_list]
string = options[:white_list].sanitize(string)
end
string = process_tidy(string)
end
string.strip!
format == :html ? "<div>#{string}</div>" : string
end
|
.tag_name_to_regex(name) ⇒ Object
169
170
171
|
# File 'lib/model_formatting.rb', line 169
def self.tag_name_to_regex(name)
%r{<#{name}[^>]*>.*?</#{name}>}m
end
|