Packages
earmark_parser
1.4.40
1.4.46
1.4.45
1.4.44
1.4.43
1.4.42
1.4.41
1.4.40
1.4.39
1.4.38
1.4.37
1.4.36
1.4.35
1.4.34
1.4.33
1.4.32
1.4.31
1.4.30
1.4.29
1.4.28
1.4.27
1.4.26
1.4.25
1.4.24
1.4.23
1.4.22
1.4.21
1.4.20
1.4.20-pre
1.4.19
1.4.18
1.4.17
1.4.16
1.4.16-pre2
1.4.16-pre1
1.4.16-pre
1.4.15
1.4.13
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
AST parser and generator for Markdown
Current section
Files
Jump to
Current section
Files
lib/earmark_parser/helpers/attr_parser.ex
defmodule EarmarkParser.Helpers.AttrParser do
@moduledoc false
import EarmarkParser.Helpers.StringHelpers, only: [ behead: 2 ]
import EarmarkParser.Message, only: [add_message: 2]
@type errorlist :: list(String.t)
def parse_attrs(context, attrs, lnb) do
{ attrs, errors } = _parse_attrs(%{}, attrs, [], lnb)
{ add_errors(context, errors, lnb), attrs }
end
defp _parse_attrs(dict, attrs, errors, lnb) do
cond do
Regex.match?(~r{^\s*$}, attrs) -> {dict, errors}
match = Regex.run(~r{^\.(\S+)\s*}, attrs) ->
[ leader, class ] = match
Map.update(dict, "class", [ class ], &[ class | &1])
|> _parse_attrs(behead(attrs, leader), errors, lnb)
match = Regex.run(~r{^\#(\S+)\s*}, attrs) ->
[ leader, id ] = match
Map.update(dict, "id", [ id ], &[ id | &1])
|> _parse_attrs(behead(attrs, leader), errors, lnb)
# Might we being running into escape issues here too?
match = Regex.run(~r{^(\S+)=\'([^\']*)'\s*}, attrs) -> #'
[ leader, name, value ] = match
Map.update(dict, name, [ value ], &[ value | &1])
|> _parse_attrs(behead(attrs, leader), errors, lnb)
# Might we being running into escape issues here too?
match = Regex.run(~r{^(\S+)=\"([^\"]*)"\s*}, attrs) -> #"
[ leader, name, value ] = match
Map.update(dict, name, [ value ], &[ value | &1])
|> _parse_attrs(behead(attrs, leader), errors, lnb)
match = Regex.run(~r{^(\S+)=(\S+)\s*}, attrs) ->
[ leader, name, value ] = match
Map.update(dict, name, [ value ], &[ value | &1])
|> _parse_attrs(behead(attrs, leader), errors, lnb)
match = Regex.run(~r{^(\S+)\s*(.*)}, attrs) ->
[ _, incorrect, rest ] = match
_parse_attrs(dict, rest, [ incorrect | errors ], lnb)
:otherwise ->
{dict, [attrs | errors ]}
end
end
defp add_errors(context, [], _lnb), do: context
defp add_errors(context, errors, lnb), do: add_message(context, {:warning, lnb, "Illegal attributes #{inspect errors} ignored in IAL"})
end
# SPDX-License-Identifier: Apache-2.0