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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 141 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
defmodule EarmarkParser.LineScanner do
@moduledoc false
alias EarmarkParser.{Helpers, Line, Options}
import EarmarkParser.LineScanner.Rgx
# This is the re that matches the ridiculous "[id]: url title" syntax
@doc false
def void_tag?(tag) do
Regex.match?(void_tag_rgx(), "<#{tag}>")
end
def scan_lines(lines, options, recursive) do
_lines_with_count(lines, options.line - 1)
|> _with_lookahead(options, recursive)
end
def type_of(line, recursive)
when is_boolean(recursive) do
type_of(line, %Options{}, recursive)
end
def type_of({line, lnb}, options = %Options{annotations: annotations}, recursive)
when is_binary(line) do
{line1, annotation} = line |> Helpers.expand_tabs() |> Helpers.remove_line_ending(annotations)
%{_type_of(line1, options, recursive) | annotation: annotation, lnb: lnb}
end
def type_of({line, lnb}, _, _) do
raise ArgumentError, "line number #{lnb} #{inspect(line)} is not a binary"
end
defp _type_of(line, options = %Options{}, recursive) do
{ial, stripped_line} = Helpers.extract_ial(line)
{content, indent} = _count_indent(line, 0)
lt_four? = indent < 4
cond do
content == "" ->
_create_text(line, content, indent)
lt_four? && !recursive && html_comment_matches(content) ->
%Line.HtmlComment{complete: true, indent: indent, line: line}
lt_four? && !recursive && html_comment_start_matches(content) ->
%Line.HtmlComment{complete: false, indent: indent, line: line}
lt_four? && dash_ruler_matches(content) ->
%Line.Ruler{type: "-", indent: indent, line: line}
lt_four? && star_ruler_matches(content) ->
%Line.Ruler{type: "*", indent: indent, line: line}
lt_four? && underline_ruler_matches(content) ->
%Line.Ruler{type: "_", indent: indent, line: line}
match = heading_matches(stripped_line) ->
[_, level, heading] = match
%Line.Heading{
level: String.length(level),
content: String.trim(heading),
indent: 0,
ial: ial,
line: line
}
match = lt_four? && block_quote_matches(content) ->
[_, quote] = match
%Line.BlockQuote{content: quote, indent: indent, ial: ial, line: line}
match = indent_matches(line) ->
[_, spaces, more_spaces, rest] = match
sl = byte_size(spaces)
%Line.Indent{
level: div(sl, 4),
content: more_spaces <> rest,
indent: byte_size(more_spaces) + sl,
line: line
}
match = fence_matches(line) ->
[_, leading, fence, language] = match
%Line.Fence{
delimiter: fence,
language: _attribute_escape(language),
indent: byte_size(leading),
line: line
}
# Although no block tags I still think they should close a preceding para as do many other
# implementations.
match = !recursive && void_tag_matches(line) ->
[_, tag] = match
%Line.HtmlOneLine{tag: tag, content: line, indent: 0, line: line}
match = !recursive && html_one_line_matches(line) ->
[_, tag] = match
%Line.HtmlOneLine{tag: tag, content: line, indent: 0, line: line}
match = !recursive && html_self_closing_matches(line) ->
[_, tag] = match
%Line.HtmlOneLine{tag: tag, content: line, indent: 0, line: line}
match = !recursive && html_open_tag_matches(line) ->
[_, tag] = match
%Line.HtmlOpenTag{tag: tag, content: line, indent: 0, line: line}
match = lt_four? && !recursive && html_close_tag_matches(content) ->
[_, tag] = match
%Line.HtmlCloseTag{tag: tag, indent: indent, line: line}
match = lt_four? && id_def_matches(content) ->
[_, id, url | title] = match
title =
if(Enum.empty?(title)) do
""
else
hd(title)
end
%Line.IdDef{id: id, url: url, title: title, indent: indent, line: line}
match = options.footnotes && footnote_def_matches(line) ->
[_, id, first_line] = match
%Line.FnDef{id: id, content: first_line, indent: 0, line: line}
match = lt_four? && bullet_list_item_matches(content) ->
[_, bullet, spaces, text] = match
%Line.ListItem{
type: :ul,
bullet: bullet,
content: spaces <> text,
indent: indent,
list_indent: String.length(bullet <> spaces) + indent + 1,
line: line
}
match = lt_four? && numbered_list_item_matches(content) ->
_create_list_item(match, indent, line)
match = table_line_matches(content) ->
[body] = match
body =
body
|> String.trim()
|> String.trim("|")
columns = _split_table_columns(body)
%Line.TableLine{
content: line,
columns: columns,
is_header: _determine_if_header(columns),
indent: indent,
line: line
}
line
|> String.replace(table_header_rgx(), "")
|> String.match?(table_first_column_rgx()) ->
columns = _split_table_columns(line)
%Line.TableLine{
content: line,
columns: columns,
is_header: _determine_if_header(columns),
indent: indent,
line: line
}
options.gfm_tables &&
line |> String.replace(table_header_rgx(), "") |> String.match?(table_column_rgx()) ->
columns = _split_table_columns(line)
%Line.TableLine{
content: line,
columns: columns,
is_header: _determine_if_header(columns),
needs_header: true,
indent: indent,
line: line
}
match = set_ext_underline_matches(line) ->
[_, type] = match
level =
if(String.starts_with?(type, "=")) do
1
else
2
end
%Line.SetextUnderlineHeading{level: level, indent: 0, line: line}
match = lt_four? && ial_matches(content) ->
[_, ial] = match
%Line.Ial{attrs: String.trim(ial), verbatim: ial, indent: indent, line: line}
true ->
_create_text(line, content, indent)
end
end
defp _attribute_escape(string) do
string
|> String.replace("&", "&")
|> String.replace("<", "<")
end
defp _create_list_item(match, indent, line)
defp _create_list_item([_, bullet, spaces, text], indent, line) do
sl = byte_size(spaces)
sl1 =
if sl > 3 do
1
else
sl + 1
end
sl2 = sl1 + byte_size(bullet)
%Line.ListItem{
type: :ol,
bullet: bullet,
content: spaces <> text,
indent: indent,
list_indent: indent + sl2,
line: line
}
end
defp _create_text(line) do
{content, indent} = _count_indent(line, 0)
_create_text(line, content, indent)
end
defp _create_text(line, "", indent) do
%Line.Blank{indent: indent, line: line}
end
defp _create_text(line, content, indent) do
%Line.Text{content: content, indent: indent, line: line}
end
defp _count_indent(<<space, rest::binary>>, indent) when space in [?\s, ?\t] do
_count_indent(rest, indent + 1)
end
defp _count_indent(rest, indent) do
{rest, indent}
end
defp _lines_with_count(lines, offset) do
Enum.zip(lines, offset..(offset + Enum.count(lines)))
end
defp _with_lookahead([line_lnb | lines], options, recursive) do
case type_of(line_lnb, options, recursive) do
%Line.Fence{delimiter: delimiter, indent: 0} = fence ->
stop = ~r/\A (\s*) (?: #{delimiter} ) \s* ([^`\s]*) \s* \z/xu
[fence | _lookahead_until_match(lines, stop, options, recursive)]
%Line.HtmlComment{complete: false} = html_comment ->
[html_comment | _lookahead_until_match(lines, ~r/-->/u, options, recursive)]
other ->
[other | _with_lookahead(lines, options, recursive)]
end
end
defp _with_lookahead([], _options, _recursive) do
[]
end
defp _lookahead_until_match([], _, _, _) do
[]
end
defp _lookahead_until_match([{line, lnb} | lines], regex, options, recursive) do
if line =~ regex do
[type_of({line, lnb}, options, recursive) | _with_lookahead(lines, options, recursive)]
else
[
%{_create_text(line) | lnb: lnb}
| _lookahead_until_match(lines, regex, options, recursive)
]
end
end
defp _determine_if_header(columns) do
column_rgx = ~r{\A[\s|:-]+\z}
columns
|> Enum.all?(fn col -> Regex.run(column_rgx, col) end)
end
defp _split_table_columns(line) do
col_sep_rgx = ~r/\\\|/
line
|> String.split(~r{(?<!\\)\|})
|> Enum.map(&String.trim/1)
|> Enum.map(fn col -> Regex.replace(col_sep_rgx, col, "|") end)
end
end
# SPDX-License-Identifier: Apache-2.0
|