Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching to a manual parsing setup for spt
- Loading branch information
Showing
7 changed files
with
161 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
<template>Greetings, program!</template> | ||
<template> | ||
Greetings, program! | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.