Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# You typically do not need to edit this file

packages = [
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
{ name = "envoy", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "850DA9D29D2E5987735872A2B5C81035146D7FE19EFC486129E44440D03FD832" },
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
{ name = "gleam_erlang", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "F91CE62A2D011FA13341F3723DB7DB118541AAA5FE7311BD2716D018F01EF9E3" },
{ name = "gleam_stdlib", version = "0.62.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "DC8872BC0B8550F6E22F0F698CFE7F1E4BDA7312FDEB40D6C3F44C5B706C8310" },
{ name = "gleeunit", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "FDC68A8C492B1E9B429249062CD9BAC9B5538C6FBF584817205D0998C42E1DAC" },
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
{ name = "splitter", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "splitter", source = "hex", outer_checksum = "128FC521EE33B0012E3E64D5B55168586BC1B9C8D7B0D0CA223B68B0D770A547" },
{ name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" },
{ name = "gleam_stdlib", version = "0.67.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6CE3E4189A8B8EC2F73AB61A2FBDE49F159D6C9C61C49E3B3082E439F260D3D0" },
{ name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" },
{ name = "simplifile", version = "2.3.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "957E0E5B75927659F1D2A1B7B75D7B9BA96FAA8D0C53EA71C4AD9CD0C6B848F6" },
{ name = "splitter", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "splitter", source = "hex", outer_checksum = "3DFD6B6C49E61EDAF6F7B27A42054A17CFF6CA2135FF553D0CB61C234D281DD0" },
]

[requirements]
Expand Down
93 changes: 50 additions & 43 deletions src/checkmark.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import checkmark/internal/parser.{type Fence}
import gleam/dict.{type Dict}
import gleam/list
import gleam/option.{type Option}
import gleam/option.{type Option, None, Some}
import gleam/result
import gleam/string
import splitter
Expand Down Expand Up @@ -130,19 +130,19 @@ pub fn update(file: File(e)) -> Result(Nil, List(CheckError(e))) {

fn render_replacements(
contents: List(parser.Section),
replacements: Dict(String, String),
replacements: Dict(String, List(String)),
) -> String {
contents
|> list.map(fn(section) {
|> list.flat_map(fn(section) {
case section {
parser.FencedCode(content:, start_fence:, end_fence:, prefix:, ..) -> {
parser.FencedCode(lines:, start_fence:, end_fence:, prefix:, ..) -> {
case dict.get(replacements, string.trim(start_fence.info)) {
Ok(replacement) ->
render_code(start_fence, prefix, replacement, end_fence)
Error(_) -> render_code(start_fence, prefix, content, end_fence)
Error(_) -> render_code(start_fence, prefix, lines, end_fence)
}
}
parser.Other(_, content) -> content
parser.Other(lines:, ..) -> lines
}
})
|> string.join("")
Expand All @@ -151,42 +151,31 @@ fn render_replacements(
fn render_code(
start_fence: Fence,
prefix: String,
content: String,
content: List(String),
end_fence: Option(Fence),
) -> String {
// This is not very optimized, but we probably don't need to care...
) -> List(String) {
let end_fence = option.map(end_fence, render_fence(prefix, _))
let without_end_fence = case start_fence.indent, prefix, end_fence {
0, "", None -> content
0, "", Some(end_fence) -> list.append(content, [end_fence])
amount, _, _ -> {
let indent = prefix <> string.repeat(" ", amount)
let content =
content
|> list.fold([], fn(lines, line) {
[string.append(indent, line), ..lines]
})

let content = case start_fence.indent, prefix {
0, "" -> content
amount, _ ->
indent(
splitter.new(["\n", "\r\n"]),
prefix <> string.repeat(" ", amount),
content,
[],
)
}

let without_end = render_fence(prefix, start_fence) <> content
case end_fence {
option.None -> without_end
option.Some(end_fence) -> without_end <> render_fence(prefix, end_fence)
}
}
let content = case end_fence {
None -> content
Some(fence) -> [fence, ..content]
}

fn indent(
line_ends: splitter.Splitter,
with: String,
rest: String,
parts: List(String),
) -> String {
case rest {
"" -> parts |> list.reverse |> string.join("")
_ -> {
let #(content, ending, rest) = splitter.split(line_ends, rest)
indent(line_ends, with, rest, [ending, content, with, ..parts])
list.reverse(content)
}
}

[render_fence(prefix, start_fence), ..without_end_fence]
}

fn render_fence(prefix: String, fence: Fence) -> String {
Expand All @@ -204,13 +193,31 @@ fn parse_file(
fn read_file(
checker: Checker(e),
filename: String,
) -> Result(String, CheckError(e)) {
checker.read(filename) |> result.map_error(CouldNotReadFile)
) -> Result(List(String), CheckError(e)) {
use content <- result.map(
checker.read(filename)
|> result.map_error(CouldNotReadFile),
)

splitter.new(["\n", "\r\n"]) |> to_lines(content, [])
}

fn to_lines(
splitter: splitter.Splitter,
content: String,
lines: List(String),
) -> List(String) {
let #(line, rest) = splitter.split_after(splitter, content)
let lines = [line, ..lines]
case rest {
"" -> list.reverse(lines)
_ -> to_lines(splitter, rest, lines)
}
}

fn check_one(
content: List(parser.Section),
expected: String,
expected: List(String),
tag: String,
) -> Result(Nil, CheckError(e)) {
use snippet <- result.try(find_match(content, tag, []))
Expand All @@ -221,7 +228,7 @@ fn check_one(
}

type Snippet {
Snippet(content: String, start_fence: Fence, end_fence: Option(Fence))
Snippet(content: List(String), start_fence: Fence, end_fence: Option(Fence))
}

fn find_match(
Expand All @@ -242,12 +249,12 @@ fn find_match(
}

[
parser.FencedCode(start_line:, content:, start_fence:, end_fence:, ..),
parser.FencedCode(line_number:, lines:, start_fence:, end_fence:, ..),
..rest
] -> {
case string.trim(start_fence.info) == tag {
True -> {
let result = #(start_line, Snippet(content, start_fence, end_fence))
let result = #(line_number, Snippet(lines, start_fence, end_fence))
find_match(rest, tag, [result, ..found])
}
False -> find_match(rest, tag, found)
Expand Down
Loading