From 0f0939550c675031b3ce878d15db6d5835aab8a2 Mon Sep 17 00:00:00 2001 From: Jason Pochanke Date: Sat, 18 Jul 2026 03:25:02 +0200 Subject: [PATCH] feat(hirachy) hirachy now converts to markdown Signed-off-by: Jason Pochanke --- src/helpers.gleam | 51 ++++++++++++++++++++ src/luma_markdown.gleam | 102 ++++++++++++++++++++-------------------- 2 files changed, 101 insertions(+), 52 deletions(-) create mode 100644 src/helpers.gleam diff --git a/src/helpers.gleam b/src/helpers.gleam new file mode 100644 index 0000000..3533af5 --- /dev/null +++ b/src/helpers.gleam @@ -0,0 +1,51 @@ +import gleam/list +import luma/definition + +pub type Information { + Information(lowesthirachy: Int, highesthirachy: Int) +} + +pub fn gather(instructions: List(definition.Instruction)) -> Information { + let pushes = + instructions + |> list.filter_map(fn(instruction) { + case instruction { + definition.Push(pusher) -> + case pusher { + definition.Hirachy(push) -> Ok(push) + _ -> Error(Nil) + } + _ -> Error(Nil) + } + }) + echo pushes + + let min_max = case pushes { + [] -> Error(Nil) + [first, ..rest] -> + Ok( + list.fold(rest, #(first, first), fn(acc, n) { + let #(min, max) = acc + + #( + case n < min { + True -> n + False -> min + }, + case n > max { + True -> n + False -> max + }, + ) + }), + ) + } + case min_max { + Ok(#(min, max)) -> { + Information(min, max) + } + Error(_) -> { + Information(0, 0) + } + } +} diff --git a/src/luma_markdown.gleam b/src/luma_markdown.gleam index 4b8c15e..05a23e9 100644 --- a/src/luma_markdown.gleam +++ b/src/luma_markdown.gleam @@ -1,57 +1,53 @@ -import luma/definition -import gleam/list -import gleam/io import argv +import gleam/io +import gleam/list +import gleam/string +import helpers +import luma/definition import luma/parser import simplifile -type Information { - Information(lowesthirachy: Int, - highesthirachy: Int) -} -fn gather(instructions: List(definition.Instruction))-> Information { - let pushes = - instructions - |> list.filter_map(fn(instruction) { - case instruction { - definition.Push(pusher) -> case pusher { - definition.Hirachy(push) -> Ok(push) - _ -> Error(Nil) - } - _ -> Error(Nil) + +pub fn luma_markdown( + information: helpers.Information, + contents: String, +) -> String { + echo contents + echo information + let lines = string.split(contents, "\n") + + let parsed = + list.map(lines, fn(line) -> String { + 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(instruction) -> "##" + } + }), + "", + ) } }) - echo pushes - let min_max = - case pushes { - [] -> Error(Nil) - [first, ..rest] -> - Ok( - list.fold(rest, #(first, first), fn(acc, n) { - let #(min, max) = acc - - #( - case n < min { - True -> n - False -> min - }, - case n > max { - True -> n - False -> max - }, - ) - }) - ) - } - case min_max { - Ok(#(min, max)) -> { - Information(min, max) - } - Error(_) -> { - Information(0, 0) - } - } + string.join(parsed, "\n") } + pub fn main() -> Nil { case argv.load().arguments { [filename] -> { @@ -59,11 +55,14 @@ pub fn main() -> Nil { Ok(contents) -> { //programm goes here let instructins = parser.handler(contents) - let result = case instructins { - definition.TextInstructions(instruction) -> gather(instruction) - _ -> Information(-124, 41234) + let information = case instructins { + // get informations + definition.TextInstructions(instruction) -> + helpers.gather(instruction) } - echo result + echo information + let result = luma_markdown(information, contents) + io.println(result) Nil } Error(_) -> io.println("Couldn't read file") @@ -71,5 +70,4 @@ pub fn main() -> Nil { } _ -> io.println("Usage: gleam run ") } - }