attr_parser.ex (2146B)
1 defmodule EarmarkParser.Helpers.AttrParser do 2 3 @moduledoc false 4 5 import EarmarkParser.Helpers.StringHelpers, only: [ behead: 2 ] 6 import EarmarkParser.Message, only: [add_message: 2] 7 8 9 @type errorlist :: list(String.t) 10 11 def parse_attrs(context, attrs, lnb) do 12 { attrs, errors } = _parse_attrs(%{}, attrs, [], lnb) 13 { add_errors(context, errors, lnb), attrs } 14 end 15 16 defp _parse_attrs(dict, attrs, errors, lnb) do 17 cond do 18 Regex.match?(~r{^\s*$}, attrs) -> {dict, errors} 19 20 match = Regex.run(~r{^\.(\S+)\s*}, attrs) -> 21 [ leader, class ] = match 22 Map.update(dict, "class", [ class ], &[ class | &1]) 23 |> _parse_attrs(behead(attrs, leader), errors, lnb) 24 25 match = Regex.run(~r{^\#(\S+)\s*}, attrs) -> 26 [ leader, id ] = match 27 Map.update(dict, "id", [ id ], &[ id | &1]) 28 |> _parse_attrs(behead(attrs, leader), errors, lnb) 29 30 # Might we being running into escape issues here too? 31 match = Regex.run(~r{^(\S+)=\'([^\']*)'\s*}, attrs) -> #' 32 [ leader, name, value ] = match 33 Map.update(dict, name, [ value ], &[ value | &1]) 34 |> _parse_attrs(behead(attrs, leader), errors, lnb) 35 36 # Might we being running into escape issues here too? 37 match = Regex.run(~r{^(\S+)=\"([^\"]*)"\s*}, attrs) -> #" 38 [ leader, name, value ] = match 39 Map.update(dict, name, [ value ], &[ value | &1]) 40 |> _parse_attrs(behead(attrs, leader), errors, lnb) 41 42 match = Regex.run(~r{^(\S+)=(\S+)\s*}, attrs) -> 43 [ leader, name, value ] = match 44 Map.update(dict, name, [ value ], &[ value | &1]) 45 |> _parse_attrs(behead(attrs, leader), errors, lnb) 46 47 match = Regex.run(~r{^(\S+)\s*(.*)}, attrs) -> 48 [ _, incorrect, rest ] = match 49 _parse_attrs(dict, rest, [ incorrect | errors ], lnb) 50 51 :otherwise -> 52 {dict, [attrs | errors ]} 53 end 54 end 55 56 defp add_errors(context, [], _lnb), do: context 57 defp add_errors(context, errors, lnb), do: add_message(context, {:warning, lnb, "Illegal attributes #{inspect errors} ignored in IAL"}) 58 59 end 60 61 # SPDX-License-Identifier: Apache-2.0