Skip to content

Commit

Permalink
feat: checklist item
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 3, 2023
1 parent 58d2f05 commit 91f5bff
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Features:

- [x] Headers
- [ ] Blockquotes
- [ ] Lists
- [x] unordered Lists
- [ ] todo lists
- [x] Code blocks
- [x] Code inline
- [x] Bold
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ fn main() {
let file_name = args.get(1).expect("not enough arguments");
let mut p = parser::Parser::new(&file_name);
let tokens = p.parse();

dbg!(tokens);
println!("{}", transformer::transform(tokens));
}
83 changes: 71 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Parser {
pub line: usize,
/// which pos the parser is at in the current line
pub line_pos: usize,
/// notate the previous character
pub last_char: char,
}

impl Parser {
Expand All @@ -26,6 +28,7 @@ impl Parser {
Parser {
input,
current_char,
last_char: '\0',
pos: 0,
line: 0,
line_pos: 0,
Expand All @@ -50,10 +53,11 @@ impl Parser {
self.peek() == character
}

/// advances to the next character in the input
/// advances to the next character in the input, increments position & line_postion by one
fn advance(&mut self) {
if !self.at_end() && self.pos + 1 <= self.input.len() {
self.pos += 1;
self.last_char = self.current_char;
self.current_char = self.input.chars().nth(self.pos).unwrap_or('\0');
self.line_pos += 1;
} else {
Expand Down Expand Up @@ -101,6 +105,11 @@ impl Parser {
continue;
}
'#' => {
if self.last_char != '\n' {
last_paragraph.push(self.current_char);
self.advance();
continue;
}
if !(self.line_pos == 0 || self.line_pos == 1) {
last_paragraph.push(self.current_char);
self.advance();
Expand Down Expand Up @@ -186,24 +195,74 @@ impl Parser {
self.advance();
}
'-' => {
if self.last_char != '\n' {
last_paragraph.push(self.current_char);
self.advance();
continue;
}
let mut minus_amount = 1;
self.advance();

while self.current_char == '-' {
minus_amount += 1;
self.advance();
}
if minus_amount >= 3 {
if !last_paragraph.is_empty() {
res.push(self.create_paragraph(&last_paragraph));
last_paragraph = String::new();

match minus_amount {
x if x >= 3 => {
if !last_paragraph.is_empty() {
res.push(self.create_paragraph(&last_paragraph));
last_paragraph = String::new();
}
res.push(self.create_token(token::TokenKind::Ruler, String::new()));
self.advance();
continue;
}
1 => {
self.advance();
match self.current_char {
// match todo list
'[' => {
if !last_paragraph.is_empty() {
res.push(self.create_paragraph(&last_paragraph));
last_paragraph = String::new();
}
// skip [
self.advance();
// if current char is x, list is done, otherwise not done
token_kind =
token::TokenKind::CheckListItem(self.current_char == 'x');
// advance to ]
self.advance();
// advance to line content
self.advance();
while self.current_char != '\n' {
token_value.push(self.current_char);
self.advance();
}
res.push(self.create_token(token_kind, token_value));
continue;
}
// match unordered list
_ => {
if !last_paragraph.is_empty() {
res.push(self.create_paragraph(&last_paragraph));
last_paragraph = String::new();
}
while self.current_char != '\n' {
token_value.push(self.current_char);
self.advance();
}
res.push(
self.create_token(token::TokenKind::Listitem, token_value),
);
continue;
}
}
}
_ => {
continue;
}
res.push(self.create_token(token::TokenKind::Ruler, String::new()));
self.advance();
continue;
} else {
last_paragraph.push_str(&"-".repeat(minus_amount));
self.advance();
continue;
}
}
'*' => {
Expand Down
4 changes: 4 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub enum TokenKind {
Quote,
/// <li>list element</li> / - list element
Listitem,
/// <li>list element</li> / - list element
OrderedListitem,
/// <li><input type="checkbox" disabled>list element</input></li> / - list element
CheckListItem(bool),
/// <code>code</code> / `code`
CodeInline,
/// <pre><code>console.log("test")</code></pre> / ```js\nconsole.log("test")```
Expand Down
8 changes: 8 additions & 0 deletions src/transformer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::token::Token;

pub fn transform(tokens: Vec<Token>) -> String {
for token in tokens {
dbg!(token);
}
return String::new();
}
4 changes: 4 additions & 0 deletions test.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ This paragraph contains _italic_ and **bold** elements.
##### Heading 5

###### Heading 6

- list
- [x] checked list
- [ ] unchecked list

0 comments on commit 91f5bff

Please sign in to comment.