Skip to content

Commit 26ab15e

Browse files
committed
transforms, claytext: lift out Transforms::INLINE
1 parent 0efb2ae commit 26ab15e

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

lib/clayoven/claytext.rb

+9-17
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,11 @@ def self.line_transforms!(paragraphs)
7979
end
8080
end
8181

82-
# We only HTML escape very few things, for simplicity
83-
HTMLESCAPE_RULES = { "&" => "&amp;", "<" => "&lt;", ">" => "&gt;" }.freeze
84-
85-
# Insert <{mark, strong, em, a, br}> into the paragraph after escaping HTML
82+
# Perform the transforms in Clayoven::Claytext::Transforms::INLINE on
83+
# Paragraph entries in-place
8684
def self.inline_transforms!(paragraphs)
87-
paragraphs.each do |p|
88-
p.replace p
89-
.gsub(/[<>&]/, HTMLESCAPE_RULES)
90-
.gsub(/`([^`]+)`/, '<mark>\1</mark>')
91-
.gsub(/!\{([^\}]+)\}/, '<strong>\1</strong>')
92-
.gsub(/!_\{([^\}]+)\}/, '<em>\1</em>')
93-
.gsub(/\[([^\[\]]+)\]\(([^)]+)\)/, '<a href="\2">\1</a>')
94-
.gsub("\u{23CE}", "<br>")
85+
Transforms::INLINE.each do |regex, replacement|
86+
paragraphs.each { |p| p.gsub! regex, replacement }
9587
end
9688
end
9789

@@ -110,11 +102,11 @@ def self.process(body)
110102
line_transforms! (paragraphs.filter { |p| p.type == :plain })
111103

112104
# Finally, do inline transforms on paragraphs untouched by the fenced transforms
113-
inline_transforms! (
114-
paragraphs.reject { |p|
115-
%i[codeblock images mathjax].count(p.type).positive?
116-
}
117-
)
105+
filtered_paragraphs =
106+
paragraphs.reject do |p|
107+
%i[codeblock images mathjax].count(p.type).positive?
108+
end
109+
inline_transforms! filtered_paragraphs
118110

119111
# Result: paragraphs
120112
paragraphs

lib/clayoven/transforms.rb

+15
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,19 @@ module Clayoven::Claytext::Transforms
124124
p.replace ["$$", XYMATRIX_START, p.to_s, XYMATRIX_END, "$$"].join("\n")
125125
end
126126
}.freeze
127+
128+
# Inline transforms
129+
#
130+
# Regex replacements for HTML escape, <{mark, strong, em, a, br}>.
131+
# We only HTML escape very few things, for simplicity
132+
INLINE = {
133+
"&" => "&amp;",
134+
"<" => "&lt;",
135+
">" => "&gt;",
136+
/`([^`]+)`/ => '<mark>\1</mark>',
137+
/!\{([^\}]+)\}/ => '<strong>\1</strong>',
138+
/!_\{([^\}]+)\}/ => '<em>\1</em>',
139+
/\[([^\[\]]+)\]\(([^)]+)\)/ => '<a href="\2">\1</a>',
140+
"\u{23CE}" => "<br>"
141+
}.freeze
127142
end

0 commit comments

Comments
 (0)