-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ex
42 lines (31 loc) · 1011 Bytes
/
parser.ex
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
defmodule Simplates.Parser do
@moduledoc """
Handles blocking out a file into blocks of templates/scripts
Then it's up to specline to the paginator/specline
"""
@block_regex ~r/(^\<template(.*)\>|^\<script\>)|(^\<\/template\>|^\<\/script\>)/im
def parse(input) do
input |> String.trim() |> String.split("\n") |> find_block()
end
def find_block(lines) do
find_block(lines, 0, [], [])
end
def find_block(remaining, found, in_block, blocks) when found == 2 do
in_block = Enum.join(in_block, "\n")
find_block(remaining, 0, [], blocks ++ [in_block])
end
def find_block([], _found, _in_block, blocks) do
blocks
end
def find_block(remaining, found, in_block, blocks) do
line = hd(remaining)
regex_matches = Regex.match?(@block_regex, line)
in_block = in_block ++ [line]
found =
case regex_matches do
true -> found + 1
false -> found
end
find_block(Enum.drop(remaining, 1), found, in_block, blocks)
end
end