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
|
defmodule EarmarkParser.Parser.ListParser do
alias EarmarkParser.{Block, Line, Options}
alias EarmarkParser.Parser.ListInfo
import EarmarkParser.Helpers.StringHelpers, only: [behead: 2]
import EarmarkParser.Message, only: [add_message: 2]
import ListInfo
@moduledoc false
@not_pending {nil, 0}
def parse_list(lines, result, options \\ %Options{}) do
{items, rest, options1} = _parse_list_items_init(lines, [], options)
list = _make_list(items, _empty_list(items))
{[list | result], rest, options1}
end
defp _parse_list_items_init([item | rest], list_items, options) do
options1 = %{options | line: item.lnb}
_parse_list_items_start(rest, _make_and_prepend_list_item(item, list_items), new(item, options1))
end
defp _parse_list_items_spaced(input, items, list_info)
defp _parse_list_items_spaced(input, items, %{pending: @not_pending} = list_info) do
_parse_list_items_spaced_np(input, items, list_info)
end
defp _parse_list_items_spaced(input, items, list_info) do
_parse_list_items_spaced_pdg(input, items, list_info)
end
defp _parse_list_items_spaced_np([%Line.Blank{} | rest], items, list_info) do
list_info1 = %{
list_info
| lines: ["" | list_info.lines],
options: %{list_info.options | line: list_info.options.line + 1}
}
_parse_list_items_spaced_np(rest, items, list_info1)
end
defp _parse_list_items_spaced_np([%Line.Ruler{} | _] = lines, items, list_info) do
_finish_list_items(lines, items, false, list_info)
end
defp _parse_list_items_spaced_np([%Line.ListItem{indent: ii} = item | _] = input, list_items, %{width: w} = list_info)
when ii < w do
if _starts_list?(item, list_items) do
_finish_list_items(input, list_items, false, list_info)
else
{items1, options1} = _finish_list_item(list_items, false, _loose(list_info))
_parse_list_items_init(input, items1, options1)
end
end
defp _parse_list_items_spaced_np([%Line.Indent{indent: ii} = item | rest], list_items, %{width: w} = list_info)
when ii >= w do
indented = _behead_spaces(item.line, w)
_parse_list_items_spaced(rest, list_items, update_list_info(list_info, indented, item, true))
end
defp _parse_list_items_spaced_np([%Line.ListItem{} = line | rest], items, list_info) do
indented = _behead_spaces(line.line, list_info.width)
_parse_list_items_start(rest, items, update_list_info(list_info, indented, line))
end
# BUG: Still do not know how much to indent here???
defp _parse_list_items_spaced_np(
[%{indent: indent, line: str_line} = line | rest],
items,
%{width: width} = list_info
)
when indent >= width do
_parse_list_items_spaced(rest, items, update_list_info(list_info, behead(str_line, width), line, true))
end
defp _parse_list_items_spaced_np(input, items, list_info) do
_finish_list_items(input, items, false, list_info)
end
defp _parse_list_items_spaced_pdg(input, items, list_info)
defp _parse_list_items_spaced_pdg([], items, %{pending: {pending, lnb}} = list_info) do
options1 =
add_message(list_info.options, {:warning, lnb, "Closing unclosed backquotes #{pending} at end of input"})
_finish_list_items([], items, false, %{list_info | options: options1})
end
defp _parse_list_items_spaced_pdg([line | rest], items, list_info) do
indented = _behead_spaces(line.line, list_info.width)
_parse_list_items_spaced(rest, items, update_list_info(list_info, indented, line, true))
end
defp _parse_list_items_start(input, list_items, list_info)
defp _parse_list_items_start(input, list_items, %{pending: @not_pending} = list_info) do
_parse_list_items_start_np(input, list_items, list_info)
end
defp _parse_list_items_start(input, list_items, list_info) do
_parse_list_items_start_pdg(input, list_items, list_info)
end
defp _parse_list_items_start_np(input, list_items, list_info)
defp _parse_list_items_start_np([%Line.Blank{} | input], items, list_info) do
_parse_list_items_spaced(input, items, prepend_line(list_info, ""))
end
defp _parse_list_items_start_np([], list_items, list_info) do
_finish_list_items([], list_items, true, list_info)
end
defp _parse_list_items_start_np([%Line.Ruler{} | _] = input, list_items, list_info) do
_finish_list_items(input, list_items, true, list_info)
end
defp _parse_list_items_start_np([%Line.Heading{} | _] = input, list_items, list_info) do
_finish_list_items(input, list_items, true, list_info)
end
defp _parse_list_items_start_np([%Line.ListItem{indent: ii} = item | _] = input, list_items, %{width: w} = list_info)
when ii < w do
if _starts_list?(item, list_items) do
_finish_list_items(input, list_items, true, list_info)
else
{items1, options1} = _finish_list_item(list_items, true, list_info)
_parse_list_items_init(input, items1, options1)
end
end
# Slurp in everything else before a first blank line
defp _parse_list_items_start_np([%{line: str_line} = line | rest], items, list_info) do
indented = _behead_spaces(str_line, list_info.width)
_parse_list_items_start(rest, items, update_list_info(list_info, indented, line))
end
defp _parse_list_items_start_pdg(input, items, list_info)
defp _parse_list_items_start_pdg([], items, list_info) do
_finish_list_items([], items, true, list_info)
end
defp _parse_list_items_start_pdg([%{line: str_line} = line | rest], items, list_info) do
indented = _behead_spaces(str_line, list_info.width)
_parse_list_items_start(rest, items, update_list_info(list_info, indented, line))
end
defp _behead_spaces(str, len) do
Regex.replace(~r/\A\s{1,#{len}}/, str, "")
end
# INLINE CANDIDATE
defp _empty_list([%Block.ListItem{loose?: loose?, type: type} | _]) do
%Block.List{loose?: loose?, type: type}
end
defp _extract_start(%{bullet: bullet}) do
start_number_rgx = ~r{\A0*(\d+)\.}
case Regex.run(start_number_rgx, bullet) do
nil -> ""
[_, "1"] -> ""
[_, start] -> ~s{ start="#{start}"}
end
end
defp _finish_list_item([%Block.ListItem{} = item | items], _at_start?, list_info) do
{blocks, _, _, options1} =
list_info.lines
|> Enum.reverse()
|> EarmarkParser.Parser.parse(%{list_info.options | line: item.lnb}, :list)
loose1? = _already_loose?(items) || list_info.loose?
{[%{item | blocks: blocks, loose?: loose1?} | items], options1}
end
defp _finish_list_items(input, items, at_start?, list_info) do
{items1, options1} = _finish_list_item(items, at_start?, list_info)
{items1, input, options1}
end
defp _make_and_prepend_list_item(%Line.ListItem{bullet: bullet, lnb: lnb, type: type}, list_items) do
[%Block.ListItem{bullet: bullet, lnb: lnb, spaced?: false, type: type} | list_items]
end
defp _make_list(items, list)
defp _make_list([%Block.ListItem{bullet: bullet, lnb: lnb} = item], %Block.List{loose?: loose?} = list) do
%{list | blocks: [%{item | loose?: loose?} | list.blocks], bullet: bullet, lnb: lnb, start: _extract_start(item)}
end
defp _make_list([%Block.ListItem{} = item | rest], %Block.List{loose?: loose?} = list) do
_make_list(rest, %{list | blocks: [%{item | loose?: loose?} | list.blocks]})
end
defp _already_loose?(items)
defp _already_loose?([]) do
false
end
defp _already_loose?([%{loose?: loose?} | _]) do
loose?
end
defp _loose(list_info) do
%{list_info | loose?: true}
end
defp _starts_list?(%{bullet: bullet1}, [%Block.ListItem{bullet: bullet2} | _]) do
String.last(bullet1) != String.last(bullet2)
end
end
# SPDX-License-Identifier: Apache-2.0
|