diff --git a/src/definition.gleam b/src/definition.gleam index 68b91ea..ccf2a58 100644 --- a/src/definition.gleam +++ b/src/definition.gleam @@ -1,3 +1,4 @@ +import gleam/json import mode pub type Line { @@ -26,3 +27,51 @@ pub type State { quotestate: List(Int), ) } + +pub fn inline_to_json(inline: Inline) -> json.Json { + case inline { + Text(text) -> + json.object([ + #("type", json.string("text")), + #("value", json.string(text)), + ]) + + Push(feeling) -> + case feeling { + Marking(level) -> + json.object([ + #("type", json.string("marking")), + #("value", json.int(level)), + ]) + + Hirachy(level) -> + json.object([ + #("type", json.string("hirachy")), + #("value", json.int(level)), + ]) + + Truth(level) -> + json.object([ + #("type", json.string("truth")), + #("value", json.int(level)), + ]) + + Quote(level) -> + json.object([ + #("type", json.string("quote")), + #("value", json.int(level)), + ]) + } + + Pop(text) -> + json.object([ + #("type", json.string("pop")), + #("value", case text { + mode.Hirachy -> json.string("Hirachy") + mode.Truth -> json.string("Truth") + mode.Marking -> json.string("Marking") + mode.Quote -> json.string("Quote") + }), + ]) + } +} diff --git a/src/markdown2.gleam b/src/markdown2.gleam index 61daf4a..aa1cfa4 100644 --- a/src/markdown2.gleam +++ b/src/markdown2.gleam @@ -1,61 +1,20 @@ -import mode import definition +import gleam/io import gleam/json import parser -fn inline_to_json(inline: definition.Inline) -> json.Json { - case inline { - definition.Text(text) -> - json.object([ - #("type", json.string("text")), - #("value", json.string(text)), - ]) - definition.Push(feeling) -> - case feeling { - definition.Marking(level) -> - json.object([ - #("type", json.string("marking")), - #("value", json.int(level)), - ]) - - definition.Hirachy(level) -> - json.object([ - #("type", json.string("hirachy")), - #("value", json.int(level)), - ]) - - definition.Truth(level) -> - json.object([ - #("type", json.string("truth")), - #("value", json.int(level)), - ]) - - definition.Quote(level) -> - json.object([ - #("type", json.string("quote")), - #("value", json.int(level)), - ]) - } - - definition.Pop(text) -> - json.object([ - #("type", json.string("pop")), - #("value", case text { - mode.Hirachy -> json.string("Hirachy") - mode.Truth -> json.string("Truth") - mode.Marking -> json.string("Marking") - mode.Quote -> json.string("Quote") - } -), - ]) - } +pub fn main() -> Nil { + io.print("This is a debug function only used for testing") + io.print(parse_to_json("!2 @3 important >1 quoted")) } fn line_to_json(line: definition.Line) -> String { let definition.Line(inlines) = line - json.array(inlines, inline_to_json) - |> json.to_string} + json.array(inlines, definition.inline_to_json) + |> json.to_string +} + pub fn parse(input: String) -> definition.Line { parser.handler(input) } diff --git a/src/parser.gleam b/src/parser.gleam index d462ffc..71aea40 100644 --- a/src/parser.gleam +++ b/src/parser.gleam @@ -1,23 +1,25 @@ import definition -import mode -import gleam/int -import gleam/result -import gleam/list import gleam/dict +import gleam/int +import gleam/list +import gleam/result import gleam/string +import mode + pub fn handler(line: String) -> definition.Line { let result = parse(line) - let result = echo list.map(result, whatsthat) + let result = list.map(result, whatsthat) definition.Line(result) } fn whatsthat(mod: String) -> definition.Inline { - let modes = dict.from_list([ - #("!", #(mode.Marking, definition.Marking)), - #("@", #(mode.Truth, definition.Truth)), - #(">", #(mode.Quote, definition.Quote)), - #("#", #(mode.Hirachy, definition.Hirachy)), - ]) + let modes = + dict.from_list([ + #("!", #(mode.Marking, definition.Marking)), + #("@", #(mode.Truth, definition.Truth)), + #(">", #(mode.Quote, definition.Quote)), + #("#", #(mode.Hirachy, definition.Hirachy)), + ]) case string.pop_grapheme(mod) { Ok(#(prefix, rest)) -> { @@ -38,7 +40,9 @@ fn whatsthat(mod: String) -> definition.Inline { Error(_) -> definition.Text(mod) } -}fn parse(modificators: String) -> List(String) { +} + +fn parse(modificators: String) -> List(String) { modificators // 1. Target the tags + their trailing space, wrapping them in "|" |> string.replace(each: "#", with: " |#")