From f0d7375f0e1450caaa89f3b66879d6640c76483f Mon Sep 17 00:00:00 2001 From: Max Value Date: Sun, 18 May 2025 14:06:27 +0100 Subject: [PATCH] Added images and links ~ Changed stylesheets ~ Changed deps --- Cargo.toml | 2 +- default.css | 14 ++++--- goodnight.gn | 95 +++++++++++++++++++++++++++++++++--------- src/inline.rs | 61 ++++++++++++++++++++------- src/parser.rs | 52 ++++++++++++++++++++++- src/types.rs | 2 + src/types/heading.rs | 10 ++--- src/types/image.rs | 45 ++++++++++++++++++++ src/types/link.rs | 79 +++++++++++++++++++++++++++++++++++ src/types/paragraph.rs | 4 +- 10 files changed, 316 insertions(+), 48 deletions(-) create mode 100644 src/types/image.rs create mode 100644 src/types/link.rs diff --git a/Cargo.toml b/Cargo.toml index 3317cf3..2ef0011 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] chrono = "0.4.41" -regex = "1.11.1" +regex = "1" serde_json = "1.0.140" diff --git a/default.css b/default.css index b3a9cfb..0594d6b 100644 --- a/default.css +++ b/default.css @@ -10,14 +10,16 @@ main { } code { - color: white; - background-color: black; + background-color: #EEE; padding: 1px 4px 1px 4px; - border-radius: 4px; + /*border-radius: 4px;*/ } pre { - color: white; - background-color: black; + background-color: #EEE; padding: 4px 4px 4px 4px; - border-radius: 4px; + /*border-radius: 4px;*/ +} + +img { + width: 100%; } diff --git a/goodnight.gn b/goodnight.gn index ff75da8..cb9d5fd 100644 --- a/goodnight.gn +++ b/goodnight.gn @@ -38,16 +38,16 @@ Example: ## Goodnight! ``` -In HTML: +In LaTeX: ``` -

Goodnight!

+\subsection{Goodnight!} ``` -In LaTeX: +In HTML: ``` -\subsection{Goodnight!} +

Goodnight!

``` In gemtext: @@ -58,53 +58,106 @@ In gemtext: ## Links -Still to be implemented +Links look similar to how images are implemented in markdown, but instead of an exclaimation mark, `=` is used. This can only be done at the start of a line. There are also more feilds than that of images, these mean the following: + +``` +=[Caption][LaTeX link][HTML link][gemtext link] + +or... -## Email addresses +=[LaTeX link][HTML link][gemtext link] -Still to be implemented +or... + +=[Caption][Universal link] + +or... + +=[Universal link] +``` + +This is because different formats might require different link locations. Format agnostic links are still possible for ease of writing or if the file in specific to one format. ## Paragraphs A paragraph is any line that does not start with one of the following: `#-.` or `\`\`\`` or one of the macros such as `[TOC]` -## Images & other external files +## Images -Still to be implemented +As in conventional markdown, images are represented with square backets, preceeded by an exclaimation mark. This must begin on the first character of a newline. The image caption is captured in the first set of square brackets, with the path to the image captured in the second. In LaTeX, this renders as an image centered in a figure enviroment with the figure caption contained in the first pair of brackets. In HTML this renders as an image in a `
` tag, with a `
` containing the caption. In gemtext this is simply a link to the path provided with the caption as the link text. + +Example: + +``` +![Goodnight][images/goodnight.png] +``` + +In LaTeX: + +``` +\begin{figure} + \centering + \includegraphics[width=0.7\textwidth]{images/goodnight.png} + \caption{Goodnight} +\end{figure} +``` + +In HTML: + +``` +
+ +
Goodnight
+
+``` + +In gemtext: + +``` +=> Goodnight (image) +``` ## Code Inline code blocks can be used with the `\`` backtick character. In LaTeX this becomes `\\texttt{...}`, in html this becomes `...` and in gemtext it is included as written. A bar character cannot currently be used in an inline code block. -Code blocks can be used with 3 backtick characters: `\`\`\``. This sequence must start on the first character on a new line. +Code blocks can be used with three backtick characters: `\`\`\``. This sequence must start on the first character on a new line. This is parsed into the `
` HTML tag, the `verbatim` LaTeX enviroment and the gemtext code block enviroment (also bookended by three backticks).
 
 ## Block quotes
 
 Block quotes are started with the tab character `\t`. They can contain any other elements.
 
-	Here is an example of a blockquote.
+	This is a paragraph within a blockquote
 
-	- Here is a list element in a blockquote
+	- This is a list element in a blockquote
 
 	Tabbed lines with a space between them are joined into one single blockquote. If two different blockquotes are needed next to each other, a backslash at the first character of a newline can split the two up.
 
+This is parsed into the HTML `quote` tag, the LaTeX `quote` enviroment and the gemtext block quote enviroment (where every line is preceeeded by a `>` symbol).
+
 ## Inline formatting
 
-- Bold styling will work only with a single asterisk character.
-- Italic styling will work only with a single underscore character.
+Bold styling is done with a single `\*` character surrounding the bold text. This is parsed to `\\textbf` and `` respectively with inline formatting not supported in gemtext.
+
+Italic styling is done with a single `\_` character surrounding the italicised text. This is parsed to `\\textit` and `` respectively with inline formatting not supported in gemtext.
 
 ## Lists
 
-- Unordered lists will work only with the hyphen character.
-- Ordered lists will work only with the period character.
+In HTML and LaTeX, lists are parsed directly to list markup with no complications (`enumerate`, `itemize` and `\\item` for LaTeX and `
    `, `
      ` and `
    • ` for HTML). In gemtext, both type of list item is included prefixed with a `\*` character and no nesting is needed. + +Ordered lists will work only with the period character. They are numbered by the browser in the case of HTML, or externally by the LaTeX compiler in the case of LaTeX. Gemtext does not support ordered lists so the parser numbers the list elements in plaintext. Ordered lists can be broken up by including a blank line between two lists. ## Macros -To generate the title, table of contents and bibliography, the following macros can be used: +To generate the title, table of contents and bibliography, the following macros can be used at the first character of a newline: -- `[TITLE]` generates the title block +- `[META]` generates the title block - `[TOC]` generates the table of contents -- `[META]` generates the bibliography +- `[REFS]` generates the bibliography + +In LaTeX, the title and bibliography are styled by the parser, rather than the native `\\maketitle` or `\\printbibliography` command. The table of contents is generated by the LaTeX compiler natively. In HTML and gemtext, the title, table of contents and references are all styled by the parser. + +Note that in HTML, `
      ` is used for a semantic title block and the `

      ` tag is used for the title only and in gemtext the first heading level is used for the title only. ## Metadata @@ -154,3 +207,7 @@ The keys are as follows: . `issuetitle`: Issue title of the book or journal the work is in (optional, must be used with `journaltitle`) . `editor`: Author of the book or journal the work is in. Should be formatted the same as "author". (optional, must be used with `journaltitle`) . `volume`: Volume of the book or journal the work is in. Example: "7th". (optional, must be used with `journaltitle`) + +## Maths + +Math mode has yet to be implemented. diff --git a/src/inline.rs b/src/inline.rs index 5ec5a03..e690ed0 100644 --- a/src/inline.rs +++ b/src/inline.rs @@ -6,7 +6,15 @@ fn parse_inline (string: &str, symbol: char, front: &str, back: &str) -> String // so that we always get the right one moving left to right let mut front_back = [back, front].into_iter().cycle(); for (index, _) in string.rmatch_indices(symbol) { - if index == 0 || !(&text[index - 1 .. index] == "\\") { + if match &text.get(index.saturating_sub(1) .. index) { + Some(r"\") => { + match &text.get(index.saturating_sub(2) .. index.saturating_sub(1)) { + Some(r"\") => true, + _ => false + } + }, + _ => true + } { text.replace_range(index .. index + 1, front_back.next().unwrap()); } } @@ -19,12 +27,7 @@ fn de_escape (string: &str) -> String { let re = Regex::new(r"\\(?[\d\D])").unwrap(); for capture in re.captures_iter(&string).collect::>().iter().rev() { let mat = capture.name("escaped").unwrap(); - if match mat.start() > 1 { - true => {&string[mat.start()-2..mat.start()-1] != r"\"} - false => {true} - } { - new_string.replace_range(mat.start()-1..mat.end(), mat.as_str()); - } + new_string.replace_range(mat.start()-1..mat.end(), mat.as_str()); } new_string @@ -37,7 +40,7 @@ pub fn parse_inline_remove (string: &str) -> String { } pub fn parse_inline_latex (string: &str) -> String { - let mut text = string.replace(r"\\", r"\\\"); + let mut text = string.replace(r"\\", r"\\\\"); text = parse_inline(&text, '*', r"\\textbf{", "}"); text = parse_inline(&text, '_', r"\\textit{", "}"); text = parse_inline(&text, '`', r"\\texttt{", "}"); @@ -48,7 +51,9 @@ pub fn parse_inline_latex (string: &str) -> String { } pub fn parse_inline_html (string: &str) -> String { - let mut text = string + let mut text = string.to_string(); + + text = text .replace(r"&", r"&") .replace(r"<", r"<") .replace(r">", r">"); @@ -68,10 +73,38 @@ mod tests { use super::*; #[test] - fn test_formatting_parser() { - let string = parse_inline("`G_ood_n*igh*t`", '*', "B", "B"); - let string = parse_inline(&string, '_', "I", "I"); - let string = parse_inline(&string, '`', "C", "C"); - assert_eq!(string, "CGIoodInBighBtC"); + fn test_parse_inline() { + assert_eq!(r"`oo`_igh_\*\_\`", parse_inline(r"`oo`_i**_\*\_\`", '*', "g", "h")); + assert_eq!(r"`oo`night\*\_\`", parse_inline(r"`oo`_igh_\*\_\`", '_', "n", "t")); + assert_eq!(r"Goodnight\*\_\`", parse_inline(r"`oo`night\*\_\`", '`', "G", "d")); + } + + #[test] + fn test_de_escape() { + assert_eq!(r"\\abc ", de_escape(r"\\\\a\b\c\ ")); + } + + #[test] + fn test_parse_inline_remove() { + assert_eq!(r"Goodnight\*\_\`", parse_inline_remove(r"*__``Goodnight\*\_\`*")); + } + + #[test] + fn test_parse_inline_latex() { + assert_eq!(r"\textbf{Goodnight}", parse_inline_latex(r"*Goodnight*")); + assert_eq!(r"\textbf{\\goodnight{}}", parse_inline_latex(r"*\\goodnight{}*")); + assert_eq!(r"\\\textit{Goodnight \&{} Goodnight}", parse_inline_latex(r"\\_Goodnight & Goodnight_")); + } + + #[test] + fn test_parse_inline_html() { + assert_eq!(r"Goodnight", parse_inline_html(r"*Goodnight*")); + assert_eq!(r"\Goodnight", parse_inline_html(r"*\\Goodnight*")); + assert_eq!(r"<Goodnight </br> Goodnight", parse_inline_html(r"\<_Goodnight
      Goodnight_")); + } + + #[test] + fn test_parse_inline_gemtext() { + assert_eq!(r"\Goodnight", parse_inline_gemtext(r"*\\Goodnight*")); } } diff --git a/src/parser.rs b/src/parser.rs index 4ce1a64..7780d8a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,6 +8,9 @@ use crate::types::reference_list::ReferenceList; use crate::types::preformatted::Preformatted; use crate::types::blockquote::BlockQuote; use crate::types::contents::Contents; +use crate::types::image::Image; +use crate::types::link::Link; +use crate::types::link::LinkType; use crate::types::list::{Item, List}; // this gets the full text of the file from the document class (with the reference list and metadata removed) @@ -18,6 +21,8 @@ pub fn parse_body (string: String, metadata: Metadata, reference_list: Option { + let re = Regex::new(r"!\[(?[\d\w (\\\[)]*)\]\[(?[\d\w\.\/]*)\]").unwrap(); + match re.captures_at(line, 0) { + Some(captures) => { + let caption = captures.name("caption").unwrap().as_str().to_string(); + let path = captures.name("path").unwrap().as_str().to_string(); + document.push( + Box::new(Image{caption, path, count: figure_counter}) + ); + figure_counter += 1; + }, + None => () + } + }, + + // match images + ('=', '[', ..) => { + let re = Regex::new(r"=(\[[\d\w \./]*\]){1,4}").unwrap(); + match re.find_at(&line, 0) { + Some(mat) => { + let feilds: Vec<&str> = line[mat.start()+2..mat.end()-1].split("][").collect(); + document.push(Box::new(Link{link_type: match feilds.len() { // this could be neater + 1 => {LinkType::Universal(feilds[0].to_string())}, + 2 => {LinkType::UniversalCaption( + feilds[0].to_string(), + feilds[1].to_string() + )}, + 3 => {LinkType::TypedUniversal( + feilds[0].to_string(), + feilds[1].to_string(), + feilds[2].to_string() + )}, + _ => {LinkType::TypedCaptioned( + feilds[0].to_string(), + feilds[1].to_string(), + feilds[2].to_string(), + feilds[3].to_string() + )} + }})); + }, + None => () + } + }, // make everything else a paragraph _ => { diff --git a/src/types.rs b/src/types.rs index e547d3a..c0d9814 100644 --- a/src/types.rs +++ b/src/types.rs @@ -7,6 +7,8 @@ pub mod list; pub mod preformatted; pub mod blockquote; pub mod contents; +pub mod image; +pub mod link; pub trait Renderable { fn render_latex(&self) -> String; diff --git a/src/types/heading.rs b/src/types/heading.rs index 6911ded..b12b056 100644 --- a/src/types/heading.rs +++ b/src/types/heading.rs @@ -32,7 +32,7 @@ impl Renderable for Heading { 4 => "h5", _ => "h6" }; - format!("<{section} id={anchor}>{text}\n") + format!("<{section} id='{anchor}'>{text}\n") } fn render_gemtext (&self) -> String { @@ -57,16 +57,16 @@ mod tests { #[test] fn test_latex_parser() { let heading = Heading{ text: String::from("Goodnight"), level: 3}; - assert_eq!(&heading.render_latex(), "\\subsubsection{Goodnight}"); + assert_eq!(&heading.render_latex(), "\\subsubsection{Goodnight}\n\n"); } #[test] fn test_html_parser() { - let heading = Heading{ text: String::from("Goodnight"), level: 5}; - assert_eq!(&heading.render_html(), "
      Goodnight
      \n"); + let heading = Heading{ text: String::from("Goodnight Publishing"), level: 5}; + assert_eq!(&heading.render_html(), "
      Goodnight Publishing
      \n"); } #[test] fn test_gemtext_parser() { let heading = Heading{ text: String::from("Goodnight"), level: 2}; - assert_eq!(&heading.render_gemtext(), "## Goodnight"); + assert_eq!(&heading.render_gemtext(), "### Goodnight\n\n"); } } diff --git a/src/types/image.rs b/src/types/image.rs new file mode 100644 index 0000000..9929aa4 --- /dev/null +++ b/src/types/image.rs @@ -0,0 +1,45 @@ +use crate::types::Renderable; + +pub struct Image { + pub path: String, + pub caption: String, + pub count: usize +} + +impl Renderable for Image { + fn render_latex(&self) -> String { + format!("\ +\\begin{{figure}} + \\centering + \\includegraphics[width=0.7/textwidth]{{{path}}} + \\caption{{{caption}}} +\\end{{figure}}\n\n", + path = self.path, + caption = self.caption) + } + fn render_html(&self) -> String { + let anchor: String = self.caption.to_lowercase().split_whitespace().map(|s| s.chars().chain(['-'])).flatten().collect(); + format!("\ +
      + +
      Figure {count}. {caption}
      +
      \n\n", + path = self.path, + caption = self.caption, + count = self.count, + anchor = &anchor[..anchor.len()-1]) + } + fn render_gemtext(&self) -> String { + format!("\ +=> <{path}> Figure {count} (image). {caption} +\n", + path = self.path, + caption = self.caption, + count = self.count) + } + fn render_plaintext(&self) -> String { + format!("Figure {count} (image). {caption}", + caption = self.caption, + count = self.count) + } +} diff --git a/src/types/link.rs b/src/types/link.rs new file mode 100644 index 0000000..bad4f8f --- /dev/null +++ b/src/types/link.rs @@ -0,0 +1,79 @@ +use super::Renderable; + +pub enum LinkType { + Universal(String), + UniversalCaption(String, String), + TypedUniversal(String, String, String), + TypedCaptioned(String, String, String, String) +} + +pub struct Link { + pub link_type: LinkType +} + +impl Renderable for Link { + fn render_latex(&self) -> String { + match &self.link_type { + LinkType::Universal(link) => { + format!("\\url{{{link}}}\n\n") + }, + LinkType::UniversalCaption(caption, link) => { + format!("\\href{{{caption}}}{{{link}}}\n\n") + }, + LinkType::TypedUniversal(link, _, _) => { + format!("\\url{{{link}}}\n\n") + }, + LinkType::TypedCaptioned(caption, link,_, _) => { + format!("\\href{{{caption}}}{{{link}}}\n\n") + }, + } + } + fn render_html(&self) -> String { + match &self.link_type { + LinkType::Universal(link) => { + format!("{link}\n") + }, + LinkType::UniversalCaption(caption, link) => { + format!("{caption}\n") + }, + LinkType::TypedUniversal(_, link, _) => { + format!("{link}\n") + }, + LinkType::TypedCaptioned(caption, _, link, _) => { + format!("{caption}\n") + }, + } + } + fn render_gemtext(&self) -> String { + match &self.link_type { + LinkType::Universal(link) => { + format!("=> {link}\n\n") + }, + LinkType::UniversalCaption(caption, link) => { + format!("=> {link} {caption}\n\n") + }, + LinkType::TypedUniversal(_, link, _) => { + format!("=> {link}\n\n") + }, + LinkType::TypedCaptioned(caption, _, link, _) => { + format!("=> {link} {caption}\n\n") + }, + } + } + fn render_plaintext(&self) -> String { + match &self.link_type { + LinkType::Universal(link) => { + format!("\\url{{{link}}}\n\n") + }, + LinkType::UniversalCaption(caption, link) => { + format!("\\href{{{caption}}}{{{link}}}\n\n") + }, + LinkType::TypedUniversal(link, _, _) => { + format!("\\url{{{link}}}\n\n") + }, + LinkType::TypedCaptioned(caption, link,_, _) => { + format!("\\href{{{caption}}}{{{link}}}\n\n") + }, + } + } +} diff --git a/src/types/paragraph.rs b/src/types/paragraph.rs index 96cc56a..b4ca7f7 100644 --- a/src/types/paragraph.rs +++ b/src/types/paragraph.rs @@ -33,7 +33,7 @@ mod tests { #[test] fn test_latex_parser() { let paragraph = Paragraph{ text: String::from("*Goodnight*")}; - assert_eq!(¶graph.render_latex(), "\\textbf{Goodnight}"); + assert_eq!(¶graph.render_latex(), "\\textbf{Goodnight}\n\n"); } #[test] fn test_html_parser() { @@ -43,6 +43,6 @@ mod tests { #[test] fn test_gemtext_parser() { let paragraph = Paragraph{ text: String::from("`Goodnight`")}; - assert_eq!(¶graph.render_gemtext(), "Goodnight"); + assert_eq!(¶graph.render_gemtext(), "Goodnight\n\n"); } } -- 2.39.2