add support for crlf, add support for changing port

This commit is contained in:
liquidex 2023-08-28 15:30:15 +02:00
parent 4b74b3930b
commit eb79cf8cab
6 changed files with 34 additions and 13 deletions

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
*.tree text=auto eol=lf

View file

@ -23,10 +23,10 @@ cargo run -p treehouse generate
This will spit out a directory `target/site` containing the static pages. You're free to use any HTTP server you wish, but for development purposes treehouse includes one in the CLI: This will spit out a directory `target/site` containing the static pages. You're free to use any HTTP server you wish, but for development purposes treehouse includes one in the CLI:
```sh ```sh
cargo run -p treehouse generate --serve cargo run -p treehouse generate --serve 8080
``` ```
This will fire up a server on port 8080. No way to change that, sorry. Edit the source code. This will fire up a server on port 8080.
If you're developing, you may wanna use [`cargo-watch`](https://crates.io/crates/cargo-watch): If you're developing, you may wanna use [`cargo-watch`](https://crates.io/crates/cargo-watch):

View file

@ -81,6 +81,26 @@ impl<'a> Parser<'a> {
self.advance(); self.advance();
} }
fn eat_until_line_break(&mut self) {
loop {
match self.current() {
Some('\r') => {
self.advance();
if self.current() == Some('\n') {
self.advance();
break;
}
}
Some('\n') => {
self.advance();
break;
}
Some(_) => self.advance(),
None => break,
}
}
}
pub fn peek_indent_level(&mut self) -> usize { pub fn peek_indent_level(&mut self) -> usize {
let position = self.position; let position = self.position;
let indent_level = self.eat_as_long_as(' '); let indent_level = self.eat_as_long_as(' ');
@ -101,10 +121,10 @@ impl<'a> Parser<'a> {
if self.current_starts_with("```") { if self.current_starts_with("```") {
code_block = None; code_block = None;
self.position += 3; self.position += 3;
self.eat_until(|c| c == '\n'); self.eat_until_line_break();
continue; continue;
} }
self.eat_until(|c| c == '\n'); self.eat_until_line_break();
if self.current().is_none() { if self.current().is_none() {
return Err(ParseErrorKind::UnterminatedCodeBlock.at(range.clone())); return Err(ParseErrorKind::UnterminatedCodeBlock.at(range.clone()));
@ -117,14 +137,14 @@ impl<'a> Parser<'a> {
continue; continue;
} }
self.eat_until(|c| c == '\n'); self.eat_until_line_break();
let before_indentation = self.position; let before_indentation = self.position;
let line_indent_level = self.eat_as_long_as(' '); let line_indent_level = self.eat_as_long_as(' ');
let after_indentation = self.position; let after_indentation = self.position;
if self.current().map(&cond).is_some_and(identity) || self.current().is_none() { if self.current().map(&cond).is_some_and(identity) || self.current().is_none() {
self.position = before_indentation; self.position = before_indentation;
break; break;
} else if !matches!(self.current(), Some('\n')) && line_indent_level < indent_level } else if !matches!(self.current(), Some('\n') | Some('\r')) && line_indent_level < indent_level
{ {
return Err(ParseErrorKind::InconsistentIndentation { return Err(ParseErrorKind::InconsistentIndentation {
got: line_indent_level, got: line_indent_level,

View file

@ -249,14 +249,14 @@ pub fn regenerate_or_report_error(paths: &Paths<'_>) {
} }
} }
pub async fn web_server() -> anyhow::Result<()> { pub async fn web_server(port: u16) -> anyhow::Result<()> {
let app = Router::new().nest_service("/", ServeDir::new("target/site")); let app = Router::new().nest_service("/", ServeDir::new("target/site"));
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let app = app.layer(LiveReloadLayer::new()); let app = app.layer(LiveReloadLayer::new());
info!("serving on port 8080"); info!("serving on port {port}");
Ok(axum::Server::bind(&([0, 0, 0, 0], 8080).into()) Ok(axum::Server::bind(&([0, 0, 0, 0], port).into())
.serve(app.into_make_service()) .serve(app.into_make_service())
.await?) .await?)
} }

View file

@ -28,9 +28,9 @@ pub enum Command {
#[derive(Args)] #[derive(Args)]
pub struct GenerateArgs { pub struct GenerateArgs {
/// Start a web server serving the static files. Useful with `cargo watch`. /// Start a web server serving the static files on the given port. Useful with `cargo watch`.
#[clap(short, long)] #[clap(short, long)]
pub serve: bool, pub serve: Option<u16>,
} }
#[derive(Args)] #[derive(Args)]

View file

@ -35,8 +35,8 @@ async fn fallible_main() -> anyhow::Result<()> {
regenerate_or_report_error(&paths); regenerate_or_report_error(&paths);
if regenerate_args.serve { if let Some(port) = regenerate_args.serve {
generate::web_server().await?; generate::web_server(port).await?;
} }
} }