diff --git a/src/luma_markdown.gleam b/src/luma_markdown.gleam index 221518d..b173b3f 100644 --- a/src/luma_markdown.gleam +++ b/src/luma_markdown.gleam @@ -11,39 +11,66 @@ pub fn luma_markdown( information: helpers.Information, contents: String, ) -> String { - let lines = string.split(contents, "\n") + let parsed = parse_lines(contents) + render_lines(information, parsed) +} - let parsed = - list.map(lines, fn(line) -> String { +fn parse_lines(contents: String) -> List(List(definition.Instruction)) { + contents + |> string.split("\n") + |> list.map(parse_line) +} + +fn parse_line(line: String) -> List(definition.Instruction) { + case + string.contains(line, "#") + || string.contains(line, "@") + || string.contains(line, "!") + || string.contains(line, ">") + { + True -> case parser.handler(line) { - definition.TextInstructions(instruction) -> - string.join( - list.map(instruction, fn(instruction) -> String { - case instruction { - definition.Text(instruction) -> instruction - definition.Push(instruction) -> - case instruction { - definition.Hirachy(instruction) -> - string.repeat( - "#", - instruction - information.lowesthirachy + 1, - ) - definition.Marking(instruction) -> - string.repeat("*", instruction) - definition.Quote(instruction) -> - string.repeat(">", instruction) - definition.Truth(instruction) -> - string.repeat("__", instruction) - } - definition.Pop(_) -> "##" - } - }), - "", - ) + definition.TextInstructions(instructions) -> instructions } - }) + False -> + case string.trim(line) { + "" -> [] + text -> [definition.Text(text)] + } + } +} - string.join(parsed, "\n") +fn render_lines( + information: helpers.Information, + parsed: List(List(definition.Instruction)), +) -> String { + parsed + |> list.map(fn(instructions) { + render_instructions(information, instructions) + }) + |> string.join("\n") +} + +fn render_instructions( + information: helpers.Information, + instructions: List(definition.Instruction), +) -> String { + instructions + |> list.map(fn(instruction) { + case instruction { + definition.Text(instruction) -> instruction + definition.Push(instruction) -> + case instruction { + definition.Hirachy(instruction) -> + string.repeat("#", instruction - information.lowesthirachy + 1) + definition.Marking(instruction) -> string.repeat("*", instruction) + definition.Quote(instruction) -> string.repeat(">", instruction) + definition.Truth(instruction) -> string.repeat("__", instruction) + } + definition.Pop(_) -> "##" + } + }) + |> string.join("") } pub fn main() -> Nil { @@ -59,7 +86,7 @@ pub fn main() -> Nil { } } } - [] -> render(read_stdin([])) + [] -> render(read_input()) _ -> { io.println("Usage: luma_markdown [file]") Nil @@ -68,20 +95,14 @@ pub fn main() -> Nil { } fn render(contents: String) -> Nil { - let information = case parser.handler(contents) { - definition.TextInstructions(instructions) -> helpers.gather(instructions) - } - let result = luma_markdown(information, contents) + let parsed = parse_lines(contents) + let information = + parsed + |> list.flatten + |> helpers.gather + let result = render_lines(information, parsed) io.println(result) } -@external(erlang, "luma_stdin", "read_line") -fn read_line() -> String - -fn read_stdin(lines: List(String)) -> String { - let line = read_line() - case line { - "" -> lines |> list.reverse |> string.join("") - _ -> read_stdin([line, ..lines]) - } -} +@external(erlang, "luma_stdin", "read_all") +fn read_input() -> String diff --git a/src/luma_stdin.erl b/src/luma_stdin.erl index 41c3e6a..f057c03 100644 --- a/src/luma_stdin.erl +++ b/src/luma_stdin.erl @@ -1,9 +1,12 @@ -module(luma_stdin). --export([read_line/0]). +-export([read_all/0]). -read_line() -> - case io:get_line("") of - eof -> <<>>; - Line -> unicode:characters_to_binary(Line) +read_all() -> + read_all([]). + +read_all(Chunks) -> + case io:get_chars("", 65536) of + eof -> unicode:characters_to_binary(lists:reverse(Chunks)); + Chunk -> read_all([Chunk | Chunks]) end. diff --git a/test/luma_markdown_test.gleam b/test/luma_markdown_test.gleam index fba3c88..c1f3488 100644 --- a/test/luma_markdown_test.gleam +++ b/test/luma_markdown_test.gleam @@ -1,4 +1,6 @@ import gleeunit +import helpers +import luma_markdown pub fn main() -> Nil { gleeunit.main() @@ -11,3 +13,10 @@ pub fn hello_world_test() { assert greeting == "Hello, Joe!" } + +pub fn renders_plain_and_marked_lines_test() { + let information = helpers.Information(1, 1) + + assert luma_markdown.luma_markdown(information, " plain text \n!2 marked") + == "plain text\n**marked" +}