Implement bold token
This commit is contained in:
parent
36e2b5925c
commit
1faa0d5c3b
6 changed files with 84 additions and 11 deletions
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
Parseable as _,
|
||||
parser::{
|
||||
lexeme::Lexeme,
|
||||
token::{Token, oblique::Oblique},
|
||||
token::{Token, oblique::Oblique, bold::Bold},
|
||||
state::State,
|
||||
},
|
||||
},
|
||||
|
|
@ -20,6 +20,11 @@ pub fn parse(
|
|||
tokens.push(Token::Oblique(Oblique::new(!state.switches.oblique)));
|
||||
state.switches.oblique = !state.switches.oblique;
|
||||
return true;
|
||||
} else if Bold::probe(lexeme) {
|
||||
log!("Bold probed {lexeme}");
|
||||
tokens.push(Token::Bold(Bold::new(!state.switches.bold)));
|
||||
state.switches.bold = !state.switches.bold;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ pub struct State {
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct Switches {
|
||||
pub oblique: bool,
|
||||
pub bold: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
|
@ -70,7 +71,10 @@ impl Default for State {
|
|||
block: Block::None,
|
||||
},
|
||||
dom_ids: HashMap::default(),
|
||||
switches: Switches { oblique: false },
|
||||
switches: Switches {
|
||||
oblique: false,
|
||||
bold: false,
|
||||
},
|
||||
buffers: Buffers {
|
||||
anchor: AnchorBuffer {
|
||||
candidate: Anchor::default(),
|
||||
|
|
|
|||
|
|
@ -1,20 +1,22 @@
|
|||
use crate::syntax::content::Parseable as _;
|
||||
|
||||
pub mod literal;
|
||||
pub mod anchor;
|
||||
pub mod linebreak;
|
||||
pub mod paragraph;
|
||||
pub mod span;
|
||||
pub mod header;
|
||||
pub mod preformat;
|
||||
pub mod bold;
|
||||
pub mod code;
|
||||
pub mod oblique;
|
||||
pub mod list;
|
||||
pub mod header;
|
||||
pub mod item;
|
||||
pub mod linebreak;
|
||||
pub mod list;
|
||||
pub mod literal;
|
||||
pub mod oblique;
|
||||
pub mod paragraph;
|
||||
pub mod preformat;
|
||||
pub mod span;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub enum Token {
|
||||
Anchor(anchor::Anchor),
|
||||
Bold(bold::Bold),
|
||||
Code(code::Code),
|
||||
Header(header::Header),
|
||||
Item(item::Item),
|
||||
|
|
@ -31,6 +33,7 @@ impl Token {
|
|||
pub fn render(&self) -> String {
|
||||
match *self {
|
||||
Token::Anchor(ref d) => d.render(),
|
||||
Token::Bold(ref d) => d.render(),
|
||||
Token::Code(ref d) => d.render(),
|
||||
Token::Header(ref d) => d.render(),
|
||||
Token::Item(ref d) => d.render(),
|
||||
|
|
@ -49,6 +52,7 @@ impl std::fmt::Display for Token {
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let data = match *self {
|
||||
Token::Anchor(ref d) => format!("{d}"),
|
||||
Token::Bold(ref d) => format!("{d}"),
|
||||
Token::Code(ref d) => format!("{d}"),
|
||||
Token::Header(ref d) => format!("{d}"),
|
||||
Token::Item(ref d) => format!("{d}"),
|
||||
|
|
|
|||
61
src/syntax/content/parser/token/bold.rs
Normal file
61
src/syntax/content/parser/token/bold.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
use crate::{
|
||||
syntax::content::{Parseable, Lexeme},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct Bold {
|
||||
open: bool,
|
||||
}
|
||||
|
||||
impl Bold {
|
||||
pub fn new(open: bool) -> Bold {
|
||||
Bold { open }
|
||||
}
|
||||
}
|
||||
|
||||
impl Parseable for Bold {
|
||||
fn probe(lexeme: &Lexeme) -> bool {
|
||||
lexeme.text() == "*"
|
||||
}
|
||||
|
||||
fn lex(_lexeme: &Lexeme) -> Bold {
|
||||
panic!("Attempt to lex a bold tag directly from a lexeme")
|
||||
}
|
||||
|
||||
fn render(&self) -> String {
|
||||
if self.open {
|
||||
String::from("<strong>")
|
||||
} else {
|
||||
String::from("</strong>")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Bold {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let display_open_state = if self.open { "open" } else { "closed" };
|
||||
write!(f, "Bold [{display_open_state}]")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn render() {
|
||||
let code_open = Bold::new(true);
|
||||
assert_eq!(code_open.render(), "<strong>");
|
||||
|
||||
let code_closed = Bold::new(false);
|
||||
assert_eq!(code_closed.render(), "</strong>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(
|
||||
expected = "Attempt to lex a bold tag directly from a lexeme"
|
||||
)]
|
||||
fn lex() {
|
||||
Bold::lex(&Lexeme::new("", ""));
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ pub struct Item {
|
|||
impl Parseable for Item {
|
||||
fn probe(lexeme: &Lexeme) -> bool {
|
||||
(lexeme.match_as_char('-') || lexeme.match_as_char('+'))
|
||||
&& lexeme.match_next_as_char(' ')
|
||||
&& lexeme.match_next_as_char(' ')
|
||||
}
|
||||
|
||||
fn lex(_lexeme: &Lexeme) -> Item {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ impl Parseable for List {
|
|||
}
|
||||
|
||||
fn render(&self) -> String {
|
||||
|
||||
let bar = if self.open { "" } else { "/" };
|
||||
let tag = if self.ordered { "ol" } else { "ul" };
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue