Compare commits

...

5 Commits

Author SHA1 Message Date
6fa26f8a33 updated workflows
All checks were successful
Test the running changes / Test (push) Successful in 39s
2025-11-16 13:38:35 +02:00
c0999b5e9f changed executable name to gravel
Some checks failed
Test the running changes / Test (push) Successful in 38s
Build and push Docker image / build (push) Has been cancelled
2025-11-16 13:32:09 +02:00
be58e2eb79 testing build workflow
Some checks failed
Test the running changes / Test (push) Successful in 1m51s
Build and push Docker image / build (push) Has been cancelled
2025-11-15 02:09:17 +02:00
8d47704b7e refactored markdown parsing, added some documentation
All checks were successful
Test the running changes / Test (push) Successful in 40s
2025-11-15 01:41:14 +02:00
05a0b32d9b refactor md parser, TODO: parse_str
Some checks failed
Test the running changes / Test (push) Failing after 44s
2025-11-14 02:28:22 +02:00
20 changed files with 766 additions and 275 deletions

9
Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM rust:1.91.1-bookworm AS builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:12-slim
WORKDIR /app
COPY --from=builder /app/target/release/stdsrv .
CMD ["./stdsrv"]

View File

@@ -54,7 +54,9 @@ need_stdout = false
[jobs.test]
command = [
"cargo", "nextest", "run",
"--hide-progress-bar", "--failure-output", "final"
"--hide-progress-bar",
"--failure-output", "final",
"--no-fail-fast"
]
need_stdout = true
analyzer = "nextest"
@@ -62,7 +64,8 @@ analyzer = "nextest"
[jobs.nextest]
command = [
"cargo", "nextest", "run",
"--hide-progress-bar", "--failure-output", "final"
"--hide-progress-bar",
"--failure-output", "final",
]
need_stdout = true
analyzer = "nextest"

View File

@@ -1,19 +1,10 @@
use crate::to_html::ToHtml;
//! Abstract syntax tree of "Markdown".
#[derive(Debug, Clone, PartialEq)]
pub struct Document {
pub blocks: Vec<Block>,
}
impl ToHtml for Document {
fn to_html(self) -> String {
format!(
"<!doctype html><html lang=en><head></head><body>{}</body></html>",
self.blocks.to_html()
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Block {
Paragraph(Vec<Inline>),
@@ -29,24 +20,6 @@ pub enum Block {
Quote(Vec<Block>),
}
impl ToHtml for Block {
fn to_html(self) -> String {
match self {
Self::Paragraph(content) => format!("<p>{}</p>", content.to_html()),
Self::Heading { level, content } => {
format!("<h{}>{}</h{}>", level, content.to_html(), level)
}
Self::Code {
language: _,
content,
} => {
format!("<pre><code>{}</code></pre>", content)
}
_ => todo!(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ListItem {
pub blocks: Vec<Block>,
@@ -60,135 +33,3 @@ pub enum Inline {
Code(String),
Link { text: Vec<Inline>, href: String },
}
impl ToHtml for Inline {
fn to_html(self) -> String {
match self {
Self::Text(s) => s,
Self::Bold(content) => format!("<b>{}</b>", content.to_html()),
Self::Italic(content) => format!("<i>{}</i>", content.to_html()),
Self::Code(s) => format!("<code>{}</code>", s),
Self::Link { text, href } => format!("<a href=\"{}\">{}</a>", href, text.to_html()),
}
}
}
impl<T> ToHtml for Vec<T>
where
T: ToHtml,
{
fn to_html(self) -> String {
let mut rendered = String::new();
for i in self {
rendered.push_str(&i.to_html());
}
rendered
}
}
// --------------------
// TESTS
// --------------------
#[cfg(test)]
mod unit_test {
use super::*;
#[test]
fn single_header() {
let ast = Document {
blocks: vec![Block::Heading {
level: 1,
content: vec![Inline::Text("Heading 1".to_string())],
}],
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1>Heading 1</h1></body></html>"
);
}
#[test]
fn inline_bold_header() {
let ast = Document {
blocks: vec![Block::Heading {
level: 1,
content: vec![
Inline::Bold(vec![Inline::Text("Bold".to_string())]),
Inline::Text(" heading 1".to_string()),
],
}],
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1><b>Bold</b> heading 1</h1></body></html>"
);
}
#[test]
fn headings_and_paragraph_nested_code() {
let ast = Document {
blocks: vec![
Block::Heading {
level: 1,
content: vec![
Inline::Bold(vec![Inline::Text("Bold".to_string())]),
Inline::Text(" heading 1".to_string()),
],
},
Block::Heading {
level: 2,
content: vec![Inline::Text("Heading 2".to_string())],
},
Block::Paragraph(vec![
Inline::Text("run ".to_string()),
Inline::Code("sudo rm -rf /".to_string()),
Inline::Text(" on your computer".to_string()),
]),
],
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1><b>Bold</b> heading 1</h1><h2>Heading 2</h2><p>run <code>sudo rm -rf /</code> on your computer</p></body></html>"
);
}
}
#[cfg(test)]
mod convert_md_to_html_test {
use crate::parser::parse;
use crate::to_html::ToHtml;
#[test]
fn single_header() {
let md = "# Header 1";
let html = parse(md).to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1>Header 1</h1></body></html>"
);
}
#[test]
fn nested_bold_headers_and_nested_code_paragraph() {
let md = "# *Bold* header 1\n## Header 2\nrun `sudo rm -rf /` on your computer";
let html = parse(md).to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1><b>Bold</b> header 1</h1><h2>Header 2</h2><p>run <code>sudo rm -rf /</code> on your computer</p></body></html>"
);
}
}

View File

@@ -1,4 +1,8 @@
#![deny(dead_code, unused_imports)]
//! A "Markdown" parser and HTML generator. Part of a static site generator `marksmith-rs`.
//! Not following any standards, only vibes.
#![deny(unused_imports)]
#![allow(clippy::needless_pass_by_value)]
use fstools::crawl_fs;
use parser::parse;
@@ -11,12 +15,98 @@ use std::{
use to_html::ToHtml;
pub mod ast;
mod parse_trait;
pub mod parser;
pub mod to_html;
#[derive(Debug)]
pub struct MdParseError {
file: Option<PathBuf>,
line: Option<usize>,
//col: Option<usize>,
expected: String,
got: String,
}
impl MdParseError {
pub fn new(expected: impl ToString, got: impl ToString) -> Self {
Self {
file: None,
line: None,
//col: None,
expected: expected.to_string(),
got: got.to_string(),
}
}
pub fn from_line(line: usize, expected: impl ToString, got: impl ToString) -> Self {
Self {
file: None,
line: Some(line),
//col: None,
expected: expected.to_string(),
got: got.to_string(),
}
}
/*
pub fn from_col(col: usize, expected: impl ToString, got: impl ToString) -> Self {
Self {
file: None,
line: None,
col: Some(col),
expected: expected.to_string(),
got: got.to_string(),
}
}
*/
#[must_use]
pub fn set_line(self, line: usize) -> Self {
Self {
file: self.file,
line: Some(line),
//col: self.col,
expected: self.expected,
got: self.got,
}
}
#[must_use]
pub fn set_file(self, file: PathBuf) -> Self {
Self {
file: Some(file),
line: self.line,
//col: self.col,
expected: self.expected,
got: self.got,
}
}
}
impl Display for MdParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// no error message :/
let file = self.file.clone().unwrap_or("<unknown>".into());
write!(
f,
"Parse error in '{}' on line {}: expected '{}', got '{}'",
file.display(),
self.line.unwrap_or(0),
//self.col.unwrap_or(0),
self.expected,
self.got
)
}
}
impl std::error::Error for MdParseError {}
#[derive(Debug)]
pub enum Error {
InDirIsNotDir,
OutDirIsNotEmpty,
OutDirIsNotDir,
OutDirFileDeleteNotAllowed,
OutDirDirectoryInPlaceOfFile,
FileRead,
@@ -24,6 +114,7 @@ pub enum Error {
FileWrite,
FileCreate,
DirCreate,
Parse(MdParseError),
}
impl Display for Error {
@@ -32,11 +123,28 @@ impl Display for Error {
}
}
impl From<MdParseError> for Error {
fn from(value: MdParseError) -> Self {
Error::Parse(value)
}
}
impl std::error::Error for Error {}
type Result<T> = std::result::Result<T, crate::Error>;
/// Takes two directories and a force flag as parameters, generates html files to the outdir in the
/// same directory structure as the md files in indir.
///
/// # Errors
/// Anything wrong with reading files from the directories or parsing the files.
pub fn generate(indir: &PathBuf, outdir: &PathBuf, force: bool) -> Result<()> {
if !indir.is_dir() {
Err(Error::InDirIsNotDir)?;
}
if !outdir.is_dir() {
Err(Error::OutDirIsNotDir)?;
}
let files = crawl_fs(indir);
for path in files {
@@ -44,7 +152,7 @@ pub fn generate(indir: &PathBuf, outdir: &PathBuf, force: bool) -> Result<()> {
// read and parse md file
let content = fs::read_to_string(&fullpath).map_err(|_e| Error::FileRead)?;
let html = parse(&content).to_html();
let html = parse(&content)?.to_html();
// write html data to file
let mut newpath = outdir.to_owned();

View File

@@ -0,0 +1,176 @@
/*
use crate::MdParseError;
pub type Pattern<T> = Vec<PatternToken<T>>;
pub enum PatternToken<T> {
Once(T),
Optional(T),
AtLeastOnce(T),
NTimes(T),
}
/// panics: on invalid pattern
pub fn char_pattern(s: &str) -> Pattern<char> {
let mut s_chars = s.chars().peekable();
let mut pat: Pattern<char> = Vec::new();
while let Some(token) = s_chars.next() {
pat.push(if let Some(&next) = s_chars.peek() {
match next {
'?' => {
s_chars.next().unwrap();
PatternToken::Optional(token)
}
'+' => {
s_chars.next().unwrap();
PatternToken::AtLeastOnce(token)
}
'*' => {
s_chars.next().unwrap();
PatternToken::NTimes(token)
}
_ => PatternToken::Once(token),
}
} else {
PatternToken::Once(token)
});
}
pat
}
pub trait ParsePattern: Iterator + Clone {
fn parse<T>(&mut self, expect: Pattern<T>) -> Result<Vec<Self::Item>, MdParseError>
where
T: PartialEq<<Self as Iterator>::Item>,
{
let mut consumed = Vec::new();
let mut cloned = self.clone();
for pat_token in expect {
match pat_token {
PatternToken::Once(c) => {
if !cloned.next().map(|v| c == v).unwrap_or(false) {
return None;
}
}
PatternToken::Optional(c) => if cloned.peek().map(|v| c == *v).unwrap_or(false) {},
}
}
*self = cloned;
Some(consumed)
}
}
*/
pub trait Parse: Iterator + Clone {
fn follows(&mut self, token: char) -> bool;
fn parse_token(&mut self, token: char) -> bool {
if self.follows(token) {
let _ = self.next();
true
} else {
false
}
}
fn parse_str(&mut self, tokens: &str) -> bool {
let mut cloned = self.clone();
for pat_token in tokens.chars() {
if cloned.follows(pat_token) {
cloned.next();
} else {
return false;
}
}
*self = cloned;
true
}
}
impl Parse for std::iter::Peekable<std::str::Chars<'_>> {
fn follows(&mut self, token: char) -> bool {
self.peek().is_some_and(|c| c == &token)
}
}
impl Parse for std::iter::Peekable<std::iter::Enumerate<std::str::Chars<'_>>> {
fn follows(&mut self, token: char) -> bool {
self.peek().is_some_and(|&(_i, c)| c == token)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn chars_parse_follows_double() {
let mut c = "abc".chars().peekable();
assert!(c.follows('a'));
assert!(c.follows('a'));
}
#[test]
fn chars_parse_tokens() {
let mut c = "abcdef".chars().peekable();
assert!(c.parse_token('a'));
assert!(c.parse_token('b'));
}
#[test]
fn chars_parse_str() {
let mut c = "abcdef".chars().peekable();
assert!(c.parse_str("abc"));
assert!(c.parse_str("def"));
}
#[test]
fn enumerate_parse_follows_double() {
let mut c = "abc".chars().enumerate().peekable();
assert!(c.follows('a'));
assert!(c.follows('a'));
}
#[test]
fn enumerate_parse_tokens() {
let mut c = "abcdef".chars().enumerate().peekable();
assert!(c.parse_token('a'));
assert!(c.parse_token('b'));
}
#[test]
fn enumerate_parse_str() {
let mut c = "abcdef".chars().enumerate().peekable();
assert!(c.parse_str("abc"));
assert!(c.parse_str("def"));
}
#[test]
fn enumerate_parse_token_failed_not_consume() {
let mut c = "abc".chars().enumerate().peekable();
assert!(!c.parse_token('b'));
assert!(c.parse_token('a'));
}
#[test]
fn enumerate_parse_str_failed_not_consume() {
let mut c = "abcdef".chars().enumerate().peekable();
assert!(!c.parse_str("def"));
assert!(c.parse_str("abc"));
}
}

View File

@@ -1,14 +1,19 @@
//! Parse "Markdown" to AST.
mod block;
mod inline;
use block::parse_blocks;
use crate::ast::Document;
use crate::{MdParseError, ast::Document};
pub fn parse(s: &str) -> Document {
Document {
blocks: parse_blocks(s),
}
/// Parses the incoming data to a Markdown abstract syntax tree.
/// # Errors
/// This function will return an `MdParseError` when any part of the input is invalid Markdown.
pub fn parse(s: &str) -> Result<Document, MdParseError> {
Ok(Document {
blocks: parse_blocks(s)?,
})
}
#[cfg(test)]
@@ -20,7 +25,7 @@ mod test {
fn only_paragraph() {
let md = "testing paragraph";
let doc = parse(md);
let doc = parse(md).unwrap();
assert_eq!(
doc,
Document {
@@ -35,7 +40,7 @@ mod test {
fn different_headers() {
let md = "# Header 1\n## Header 2";
let doc = parse(md);
let doc = parse(md).unwrap();
assert_eq!(
doc,
@@ -58,7 +63,7 @@ mod test {
fn inline_bold_and_italics() {
let md = "some *bold* and _italic_ text";
let doc = parse(md);
let doc = parse(md).unwrap();
assert_eq!(
doc,
@@ -78,7 +83,7 @@ mod test {
fn inline_code() {
let md = "run command `sudo rm -rf /`";
let doc = parse(md);
let doc = parse(md).unwrap();
assert_eq!(
doc,
@@ -95,7 +100,7 @@ mod test {
fn bold_header() {
let md = "# Header is *bold*";
let doc = parse(md);
let doc = parse(md).unwrap();
assert_eq!(
doc,
@@ -115,7 +120,7 @@ mod test {
fn anonymous_code_block() {
let md = "```\necho hello\n```";
let doc = parse(md);
let doc = parse(md).unwrap();
assert_eq!(
doc,
@@ -132,7 +137,7 @@ mod test {
fn rust_code_block() {
let md = "```rust\nfn main() {\n\tprintln!(\"Hello world!\");\n}\n```";
let doc = parse(md);
let doc = parse(md).unwrap();
assert_eq!(
doc,
@@ -145,3 +150,4 @@ mod test {
);
}
}
// */

View File

@@ -1,22 +1,120 @@
use crate::ast::Block;
use super::inline::parse_inlines;
use crate::{MdParseError, ast::Block};
pub fn parse_blocks(input: &str) -> Vec<Block> {
use crate::parse_trait::Parse;
pub fn parse_blocks(input: &str) -> Result<Vec<Block>, MdParseError> {
let mut blocks = Vec::new();
let mut lines = input.lines().enumerate().peekable();
while let Some((i, line)) = lines.next() {
let mut line_chars = line.chars().peekable();
// empty line
if line_chars.peek().is_none() {
continue;
}
// header
let mut heading_level = 0;
while line_chars.parse_token('#') {
if heading_level < 6 {
heading_level += 1;
}
}
if heading_level > 0 {
if !line_chars.parse_token(' ') {
Err(MdParseError::from_line(
i + 1,
"<space> after #",
"no <space>",
))?;
}
let line_content: String = line_chars.collect();
blocks.push(Block::Heading {
level: heading_level,
content: parse_inlines(&line_content)?,
});
continue;
}
// quote TODO
/*
if line_chars.parse_str("> ") {
let content: String = line_chars.collect();
let quote_blocks = parse_blocks(&content).map_err(|e| e.set_line(i + 1))?;
blocks.push(Block::Quote(quote_blocks));
continue;
}
*/
// code
if line_chars.parse_str("```") {
let lang_line: String = line_chars.collect();
let lang = if lang_line.is_empty() {
None
} else {
Some(lang_line)
};
let mut code = String::new();
let mut successful = false;
for (j, line) in lines.by_ref() {
let mut code_line_chars = line.chars().peekable();
// code block end
if code_line_chars.parse_str("```") {
let remaining: String = code_line_chars.collect();
if remaining.is_empty() {
blocks.push(Block::Code {
language: lang,
content: code,
});
successful = true;
break;
}
Err(MdParseError::from_line(
j + 1,
"```",
format!("```{remaining}"),
))?;
} else {
code.push_str(line);
code.push('\n');
}
}
if successful {
continue;
}
Err(MdParseError::from_line(i + 1, "a terminating '```'", ""))?;
}
// lists TODO
// paragraph
blocks.push(Block::Paragraph(
parse_inlines(line).map_err(|e| e.set_line(i + 1))?,
));
}
Ok(blocks)
}
/*
pub fn parse_blocks(input: &str) -> Result<Vec<Block>, MdParseError> {
let mut blocks = Vec::new();
let mut lines = input.lines().peekable();
let mut lines = input.lines().enumerate().peekable();
while let Some(line) = lines.next() {
while let Some((i, line)) = lines.next() {
if line.starts_with("#") {
let level = line.chars().take_while(|&c| c == '#').count() as u8;
let text = line[level as usize..].trim();
blocks.push(Block::Heading {
level,
content: parse_inlines(text),
content: parse_inlines(text).map_err(|e| e.set_line(i + 1))?,
});
} else if let Some(quote_body) = line.strip_prefix(">") {
let quote_blocks = parse_blocks(quote_body);
let quote_blocks = parse_blocks(quote_body).map_err(|e| e.set_line(i + 1))?;
blocks.push(Block::Quote(quote_blocks));
} else if line.starts_with("```") {
let lang_line = line.strip_prefix("```").unwrap().to_string();
@@ -26,8 +124,16 @@ pub fn parse_blocks(input: &str) -> Vec<Block> {
Some(lang_line)
};
let mut code = String::new();
while lines.peek().is_some() && !lines.peek().unwrap().starts_with("```") {
code.push_str(&format!("{}\n", lines.next().unwrap()));
while lines.peek().is_some()
&& !lines
.peek()
.ok_or(MdParseError::from_line(i + 1, "a line", ""))?
.1
.starts_with("```")
{
if let Some((_i, l)) = lines.next() {
code.push_str(&format!("{}\n", l));
}
}
lines.next();
blocks.push(Block::Code {
@@ -37,9 +143,12 @@ pub fn parse_blocks(input: &str) -> Vec<Block> {
} else if line.trim().is_empty() {
continue;
} else {
blocks.push(Block::Paragraph(parse_inlines(line)));
blocks.push(Block::Paragraph(
parse_inlines(line).map_err(|e| e.set_line(i + 1))?,
));
}
}
blocks
Ok(blocks)
}
*/

View File

@@ -1,31 +1,36 @@
use crate::ast::Inline;
use crate::{MdParseError, ast::Inline};
pub fn parse_inlines(input: &str) -> Vec<Inline> {
pub fn parse_inlines(input: &str) -> Result<Vec<Inline>, MdParseError> {
let mut inlines = Vec::new();
let mut chars = input.chars().peekable();
while let Some(c) = chars.next() {
match c {
'*' => {
let inner = collect_until(&mut chars, '*');
inlines.push(Inline::Bold(parse_inlines(&inner)));
let inner = collect_until(&mut chars, '*')?;
inlines.push(Inline::Bold(parse_inlines(&inner)?));
}
'_' => {
let inner = collect_until(&mut chars, '_');
inlines.push(Inline::Italic(parse_inlines(&inner)));
let inner = collect_until(&mut chars, '_')?;
inlines.push(Inline::Italic(parse_inlines(&inner)?));
}
'`' => {
let code = collect_until(&mut chars, '`');
let code = collect_until(&mut chars, '`')?;
inlines.push(Inline::Code(code));
}
'[' => {
let text = collect_until(&mut chars, ']');
if chars.next() == Some('(') {
let href = collect_until(&mut chars, ')');
let text = collect_until(&mut chars, ']')?;
if let Some('(') = chars.next() {
let href = collect_until(&mut chars, ')')?;
inlines.push(Inline::Link {
text: parse_inlines(&text),
text: parse_inlines(&text)?,
href,
});
} else {
Err(MdParseError::new(
"(<href>)",
chars.next().unwrap_or_default(),
))?;
}
}
_ => {
@@ -35,27 +40,92 @@ pub fn parse_inlines(input: &str) -> Vec<Inline> {
if matches!(nc, '*' | '_' | '`' | '[') {
break;
}
text.push(chars.next().unwrap());
let c = chars.next().ok_or(MdParseError::new("a character", ""))?;
text.push(c);
}
inlines.push(Inline::Text(text));
}
}
}
inlines
Ok(inlines)
}
fn collect_until<I: Iterator<Item = char>>(
chars: &mut std::iter::Peekable<I>,
end: char,
) -> String {
) -> Result<String, MdParseError> {
let mut s = String::new();
while let Some(&c) = chars.peek() {
for c in chars.by_ref() {
if c == end {
chars.next();
break;
return Ok(s);
}
s.push(chars.next().unwrap());
s.push(c);
}
Err(MdParseError::new(end, ""))
}
#[cfg(test)]
mod test {
use crate::ast::Inline;
use super::parse_inlines;
#[test]
fn bold_text() {
let md = "*abc*";
let inl = parse_inlines(md).unwrap();
assert_eq!(
inl,
vec![Inline::Bold(vec![Inline::Text("abc".to_string())])]
);
}
#[test]
fn italic_text() {
let md = "_abc_";
let inl = parse_inlines(md).unwrap();
assert_eq!(
inl,
vec![Inline::Italic(vec![Inline::Text("abc".to_string())])]
);
}
#[test]
fn bold_italic_text() {
let md = "*_abc_*";
let inl = parse_inlines(md).unwrap();
assert_eq!(
inl,
vec![Inline::Bold(vec![Inline::Italic(vec![Inline::Text(
"abc".to_string()
)])])]
);
}
#[test]
fn code() {
let md = "`sudo rm -rf /`";
let inl = parse_inlines(md).unwrap();
assert_eq!(inl, vec![Inline::Code("sudo rm -rf /".to_string())]);
}
#[test]
fn text_and_code() {
let md = "run `sudo rm -rf /` on your computer";
let inl = parse_inlines(md).unwrap();
assert_eq!(
inl,
vec![
Inline::Text("run ".to_string()),
Inline::Code("sudo rm -rf /".to_string()),
Inline::Text(" on your computer".to_string())
]
);
}
s
}

View File

@@ -1,3 +1,198 @@
//! A trait + implementations for generating HTML.
use crate::ast::{Block, Document, Inline};
pub trait ToHtml {
fn to_html(self) -> String;
}
impl ToHtml for Document {
fn to_html(self) -> String {
format!(
"<!doctype html><html lang=en><head></head><body>{}</body></html>",
self.blocks.to_html()
)
}
}
impl ToHtml for Block {
fn to_html(self) -> String {
match self {
Self::Paragraph(content) => format!("<p>{}</p>", content.to_html()),
Self::Heading { level, content } => {
format!("<h{}>{}</h{}>", level, content.to_html(), level)
}
Self::Code {
language: _,
content,
} => {
format!("<pre><code>{content}</code></pre>")
}
_ => todo!(),
}
}
}
impl ToHtml for Inline {
fn to_html(self) -> String {
match self {
Self::Text(s) => s,
Self::Bold(content) => format!("<b>{}</b>", content.to_html()),
Self::Italic(content) => format!("<i>{}</i>", content.to_html()),
Self::Code(s) => format!("<code>{s}</code>"),
Self::Link { text, href } => format!("<a href=\"{}\">{}</a>", href, text.to_html()),
}
}
}
impl<T> ToHtml for Vec<T>
where
T: ToHtml,
{
fn to_html(self) -> String {
let mut rendered = String::new();
for i in self {
rendered.push_str(&i.to_html());
}
rendered
}
}
// --------------------
// TESTS
// --------------------
#[cfg(test)]
mod unit_test {
use super::*;
#[test]
fn single_header() {
let ast = Document {
blocks: vec![Block::Heading {
level: 1,
content: vec![Inline::Text("Heading 1".to_string())],
}],
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1>Heading 1</h1></body></html>"
);
}
#[test]
fn inline_bold_header() {
let ast = Document {
blocks: vec![Block::Heading {
level: 1,
content: vec![
Inline::Bold(vec![Inline::Text("Bold".to_string())]),
Inline::Text(" heading 1".to_string()),
],
}],
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1><b>Bold</b> heading 1</h1></body></html>"
);
}
#[test]
fn headings_and_paragraph_nested_code() {
let ast = Document {
blocks: vec![
Block::Heading {
level: 1,
content: vec![
Inline::Bold(vec![Inline::Text("Bold".to_string())]),
Inline::Text(" heading 1".to_string()),
],
},
Block::Heading {
level: 2,
content: vec![Inline::Text("Heading 2".to_string())],
},
Block::Paragraph(vec![
Inline::Text("run ".to_string()),
Inline::Code("sudo rm -rf /".to_string()),
Inline::Text(" on your computer".to_string()),
]),
],
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1><b>Bold</b> heading 1</h1><h2>Heading 2</h2><p>run <code>sudo rm -rf /</code> on your computer</p></body></html>"
);
}
}
#[cfg(test)]
mod convert_md_to_html_test {
use crate::parser::parse;
use crate::to_html::ToHtml;
#[test]
fn single_header() {
let md = "# Header 1";
let ast = match parse(md) {
Ok(a) => a,
Err(e) => panic!("{}", e),
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1>Header 1</h1></body></html>"
);
}
#[test]
fn single_header_wrong_format() {
let md = "#Whoops";
let ast = parse(md);
assert!(ast.is_err());
}
#[test]
fn nested_bold_headers_and_nested_code_paragraph() {
let md = "# *Bold* header 1\n## Header 2\nrun `sudo rm -rf /` on your computer";
let ast = match parse(md) {
Ok(a) => a,
Err(e) => panic!("{}", e),
};
let html = ast.to_html();
assert_eq!(
html,
"<!doctype html><html lang=en><head></head><body><h1><b>Bold</b> header 1</h1><h2>Header 2</h2><p>run <code>sudo rm -rf /</code> on your computer</p></body></html>"
);
}
}
#[cfg(test)]
mod parse_real_md {
use std::fs;
use crate::parser::parse;
#[test]
fn go() {
let file = "./test.md";
let md = fs::read_to_string(file).expect("reading ./test.md failed");
let _ast = match parse(&md).map_err(|e| e.set_file(file.into())) {
Ok(a) => a,
Err(e) => panic!("{}", e),
};
}
}

7
cracked_md/test.md Normal file
View File

@@ -0,0 +1,7 @@
# Header *1kkkkkkkkkkkkkkkkkkkkkk*
this is some code: `abc`
```code
oiajwefoijao089uaoisdjfoijasdfoijasdofij
```

View File

@@ -2,13 +2,14 @@ use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;
/// Recursively crawl through the directory given and aggregate all file handles to a HashMap with
/// Recursively crawl through the directory given and aggregate all file handles to a `HashMap` with
/// their respective (relative) paths as keys.
#[must_use]
pub fn crawl_fs(root: &PathBuf) -> HashSet<PathBuf> {
crawl_fs_rec(root, root)
}
/// Helper function: Recursively crawl through the directory given and aggregate all file handles to a HashMap with
/// Helper function: Recursively crawl through the directory given and aggregate all file handles to a `HashMap` with
/// their respective (relative) paths as keys.
fn crawl_fs_rec(root: &PathBuf, path: &PathBuf) -> HashSet<PathBuf> {
let mut subdirs = Vec::with_capacity(100);

View File

@@ -3,6 +3,10 @@ name = "stdsrv"
version = "0.1.0"
edition = "2024"
[[bin]]
name = "gravel"
path = "src/main.rs"
# local dependencies
[dependencies]
cracked_md = { path = "../cracked_md" }

View File

@@ -1,3 +1,5 @@
//! Simple and program specific command line argument parsing solution.
use std::path::PathBuf;
use crate::error::Error;

View File

@@ -1,10 +1,11 @@
//! FileServer
//! A simple server implementation that just responds with the contents of the file requested in
//! the provided directory.
use std::fs;
use std::path::PathBuf;
use crate::{
error::*,
error::{Error, ErrorKind, Result},
request::{HttpMethod, HttpRequest},
responder::Responder,
response::{HttpResponse, HttpStatus},
@@ -51,7 +52,6 @@ impl Responder for FileServer {
let content_type = match req.path.extension() {
Some(s) => match s.as_encoded_bytes() {
b"html" | b"htm" => "text/html",
b"txt" => "text/plain",
b"css" => "text/css",
b"js" => "text/javascript",
b"pdf" => "application/pdf",

View File

@@ -1,36 +1,38 @@
//! Wrapper type of a `HashMap` to contain HTTP headers and format them correctly.
use std::{collections::HashMap, fmt::Display};
#[derive(Debug)]
pub struct HttpHeaders {
_inner: HashMap<String, String>,
inner: HashMap<String, String>,
}
impl HttpHeaders {
pub fn new() -> Self {
HttpHeaders {
_inner: HashMap::new(),
inner: HashMap::new(),
}
}
pub fn add(&mut self, k: &str, v: &str) {
self._inner.insert(k.to_string(), v.to_string());
self.inner.insert(k.to_string(), v.to_string());
}
#[cfg(test)]
pub fn get(&self, k: &str) -> Option<&String> {
self._inner.get(k)
self.inner.get(k)
}
#[cfg(test)]
pub fn len(&self) -> usize {
self._inner.len()
self.inner.len()
}
}
impl Display for HttpHeaders {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (k, v) in self._inner.iter() {
write!(f, "{}: {}\r\n", k, v)?;
for (k, v) in &self.inner {
write!(f, "{k}: {v}\r\n")?;
}
Ok(())
}

View File

@@ -24,6 +24,9 @@ impl Display for Level {
}
}
/// A logging macro. Takes a [`Level`] and a formatted string.
///
/// [`Level`]: ./logger/enum.Level.html
#[macro_export]
macro_rules! log {
($level:expr, $($arg:tt)*) => {{

View File

@@ -1,6 +1,4 @@
//! This is my very monolithic file server with zero dependencies (other than rust
//! stdlib).
//!
//! A simple web server with 0 dependencies (other than Rust's stdlib).
//! Documentation is a work in progress, go see my webpage at [jlux.dev](https://jlux.dev).
use std::{
@@ -32,7 +30,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if args.generate {
match generate(&args.indir, &args.outdir, args.force) {
Ok(_) => log!(
Ok(()) => log!(
Level::Info,
"HTML generation from `{}` to `{}` successful",
args.indir.display(),
@@ -50,7 +48,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
log!(Level::Error, "HTML generation failed with error: {}", e,);
process::exit(1);
}
};
}
}
let listener = TcpListener::bind(&args.addr)?;
@@ -59,7 +57,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for stream in listener.incoming() {
match stream {
Ok(stream) => {
let outdir = args.outdir.to_owned();
let outdir = args.outdir.clone();
std::thread::spawn(move || {
log!(Level::Debug, "TcpStream handler spawned");
let mut reader = BufReader::new(&stream);

View File

@@ -32,7 +32,7 @@ impl TryFrom<&str> for HttpMethod {
impl Display for HttpMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
write!(f, "{self:?}")
}
}
@@ -154,7 +154,7 @@ impl TryFrom<&str> for HttpRequest {
for line in lines {
if let Some(v) = line.split_once(": ") {
headers.add(v.0, v.1)
headers.add(v.0, v.1);
}
}

View File

@@ -1,55 +1,12 @@
//! Traits helping HTTP connections
//! Helper trait(s).
use crate::request::HttpRequest;
use crate::response::HttpResponse;
/// Responder trait. Just a respond method that turns a HttpRequest to a HttpResponse.
/// Responder trait. Just a respond method that turns a [`HttpRequest`] to a [`HttpResponse`].
///
/// [`HttpRequest`]: ../request/struct.HttpRequest.html
/// [`HttpResponse`]: ../response/struct.HttpResponse.html
pub trait Responder {
fn respond(&self, req: HttpRequest) -> HttpResponse;
}
/*
/// Size trait. Number of bytes when encoded.
pub trait Size {
fn size(&self) -> usize;
}
// Standard implementations for Size trait
impl Size for u8 {
fn size(&self) -> usize {
1
}
}
impl<T> Size for Vec<T>
where
T: Size,
{
fn size(&self) -> usize {
if let Some(elem) = self.first() {
elem.size() * self.len()
} else {
0
}
}
}
impl<T> Size for Option<T>
where
T: Size,
{
fn size(&self) -> usize {
match self {
Some(t) => t.size(),
None => 0,
}
}
}
impl Size for String {
fn size(&self) -> usize {
self.len()
}
}
*/

View File

@@ -146,7 +146,7 @@ impl Display for HttpResponse {
write!(
f,
"{}\r\n",
String::from_utf8(s.to_vec()).unwrap_or("<binary data>".to_string())
String::from_utf8(s.clone()).unwrap_or("<binary data>".to_string())
)?;
}
Ok(())