Skip to content
Open
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
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ repos:
- id: mdsh
name: mdsh
description: README.md shell pre-processor.
entry: cargo run -- --inputs
entry: cargo run -- --inputs README.md
language: system
files: README.md
files: ^README.md$
always_run: true
pass_filenames: false
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no changes to README.md with always_run it is run without README.md being passed as the cli arg param

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<!-- END mdsh -->
<!-- END mdsh -->
2 changes: 1 addition & 1 deletion ci.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -I nixpkgs=channel:nixos-22.05
#!nix-shell -i bash -I nixpkgs=channel:nixos-25.11
# shellcheck shell=bash
#
# Travis CI specific build script
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod cli;
pub mod executor;
mod nom_ext;
pub mod parser;
#[cfg(test)]
mod tests;

use std::io::Write;

Expand All @@ -26,12 +28,15 @@ pub trait Processor<'a> {
.context("processing markdown piece")?;
}

let (_input, _) = iter
let (remaining_input, _) = iter
.finish()
.finish()
.map_err(fmt_nom_error(input, &format!("{input_pipe:?}")))
.context("parsing markdown")?;

self.process_piece(MdPiece::RawLine(remaining_input))
.context("processing remaining unparsed input")?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can happen when last character of the input is not newline. We can't add nom::combinator::rest at the end as then iterator will keep matching on it forever.


Ok(())
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ use std::{
};

use anyhow::Context;

/// Trims trailing ASCII whitespace from a byte slice.
/// Stable alternative to the unstable `trim_ascii_end()` method.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is stable since 1.80.0

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the CI cargo version is outdated

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bumped nixos to 25.11 but the rustc version there is 1.78. I don't have much experience with nix, is there any quick fix, @zimbatm? It would be nice to have the latest version features available (which is 1.92 as of today)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nixos-unstable is at 1.89.0. Is that enough?

fn trim_ascii_end(bytes: &[u8]) -> &[u8] {
let mut end = bytes.len();
while end > 0 && bytes[end - 1].is_ascii_whitespace() {
end -= 1;
}
&bytes[..end]
}
use clap::Parser;
use mdsh::{
cli::{FileArg, Opt, Parent},
Expand Down Expand Up @@ -81,8 +71,10 @@ fn process_file(
}
let file_unmodified_check = !frozen || input_content.as_bytes() == buffer;

std::fs::write(outf, trim_ascii_end(&buffer))
.with_context(|| format!("failed to write file {outf:?}"))?;
let mut out =
File::create(outf).with_context(|| format!("failed to write file {outf:?}"))?;
out.write(buffer.trim_ascii_end())?;
out.write(b"\n")?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused some parsing failures (that should be fixed). And it's incompatible with the precommit's end-of-file-fixer hook


file_unmodified_check
.then_some(())
Expand Down
19 changes: 10 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ impl<'a> nom::Parser<&'a str> for FencedBlockParser {
many0_count(not(tag(BEGIN_MDSH).or(tag(END_MDSH))).and(anychar))
.and(alt((peek(tag(END_MDSH)), recognize(Self)))),
),
cut(tag(END_MDSH)
.and(space0)
.and(recognize(newline).or(eof))
.and(multispace0)),
cut(tag(END_MDSH).and(space0).and(eolf()).and(multispace0)),
),
)
.map(|_| ())
Expand All @@ -104,7 +101,7 @@ fn link<'a>() -> impl Parser<'a, Action<'a>> {
not(char('[')),
cut((take_until("]"), tag("]("))),
cut(filepath()),
cut((char(')'), newline)),
cut((char(')'), eolf())),
),
)
.map(|(_, command, _, _, filepath, _)| Action {
Expand Down Expand Up @@ -166,7 +163,7 @@ fn actionable_comment<'a>() -> impl Parser<'a, Action<'a>> {
delimited(
tag("<!--"),
take_until1("-->"),
tag("-->").and(space0).and(newline),
tag("-->").and(space0).and(eolf()),
)
.and_then(
(
Expand Down Expand Up @@ -197,7 +194,7 @@ fn inline_code<'a>() -> impl Parser<'a, Action<'a>> {
context(
"inline code",
recognize(take_while_m_n(1, 2, |x| x == '`').and(not(char('`'))))
.flat_map(|q1| terminated(take_until1(q1), tag(q1).and(newline)))
.flat_map(|q1| terminated(take_until1(q1), tag(q1).and(eolf())))
.and_then((command(), space0, rest))
.map(|(command, _, rest)| Action {
command,
Expand All @@ -221,7 +218,7 @@ fn actionable_code_block<'a>() -> impl Parser<'a, Action<'a>> {
command(),
space0,
opt(take_until1("\n")),
newline,
eolf(),
)
.map(|(_srclang, command, _, data_line, _)| (command, data_line))
}
Expand Down Expand Up @@ -252,7 +249,7 @@ fn code_block<'a, X>(
peek(tag(q)).map(|_| "\n"), // covers the edge case with empty code block
recognize(many0_count(not(newline.and(tag(q))).and(anychar)).and(newline)),
))),
cut(tag(q).and(newline)),
cut(tag(q).and(eolf())),
)
}),
)
Expand Down Expand Up @@ -288,6 +285,10 @@ pub fn env_var_line<'a>() -> impl Parser<'a, Option<(&'a str, &'a str)>> {
))
}

fn eolf<'a>() -> impl Parser<'a, &'a str> {
recognize(newline).or(eof)
}

pub fn fmt_nom_error<'a>(
input: &'a str,
src_name: &'a str,
Expand Down
Loading