Add parsing of the list

pull/1/head
trivernis 5 years ago
parent e9e7dca783
commit c89b9d83fb

1
.gitignore vendored

@ -1,2 +1,3 @@
/target /target
**/*.rs.bk **/*.rs.bk
.idea

8
.idea/.gitignore vendored

@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

@ -1,7 +0,0 @@
<component name="ProjectDictionaryState">
<dictionary name="trivernis">
<words>
<w>striked</w>
</words>
</dictionary>
</component>

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/markdown-rs.iml" filepath="$PROJECT_DIR$/.idea/markdown-rs.iml" />
</modules>
</component>
</project>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -44,6 +44,16 @@ impl Parser {
} }
} }
pub fn seek_whitespace(&mut self) {
if self.current_char.is_whitespace() {
while let Some(next_char) = self.next_char() {
if !next_char.is_whitespace() {
break;
}
}
}
}
pub fn parse(&mut self) { pub fn parse(&mut self) {
let mut document = Document::new(); let mut document = Document::new();
while self.index < self.text.len() { while self.index < self.text.len() {
@ -73,6 +83,7 @@ impl Parser {
} }
} }
/// Parses a section that consists of a header and one or more blocks
pub fn parse_section(&mut self) -> Result<Section, ParseError> { pub fn parse_section(&mut self) -> Result<Section, ParseError> {
let start_index = self.index; let start_index = self.index;
if self.current_char == '#' { if self.current_char == '#' {
@ -89,7 +100,7 @@ impl Parser {
self.revert_to(start_index)?; self.revert_to(start_index)?;
return Err(ParseError::new(index)); return Err(ParseError::new(index));
} }
let _ = self.next_char(); self.seek_whitespace();
let mut header = self.parse_header()?; let mut header = self.parse_header()?;
header.size = size; header.size = size;
self.section_nesting = size; self.section_nesting = size;
@ -106,7 +117,16 @@ impl Parser {
} }
pub fn parse_paragraph(&mut self) -> Result<Paragraph, ParseError> { pub fn parse_paragraph(&mut self) -> Result<Paragraph, ParseError> {
unimplemented!() let mut paragraph = Paragraph::new();
while let Ok(token) = self.parse_inline() {
paragraph.add_element(token);
}
if paragraph.elements.len() > 0 {
Ok(paragraph)
} else {
Err(ParseError::new(self.index))
}
} }
pub fn parse_list(&mut self) -> Result<List, ParseError> { pub fn parse_list(&mut self) -> Result<List, ParseError> {
@ -120,10 +140,18 @@ impl Parser {
pub fn parse_header(&mut self) -> Result<Header, ParseError> { pub fn parse_header(&mut self) -> Result<Header, ParseError> {
Ok(Header { Ok(Header {
size: 0, size: 0,
text: self.parse_text()?, line: self.parse_inline()?,
}) })
} }
pub fn parse_list_item(&mut self) -> Result<ListItem, ParseError> {
unimplemented!()
}
pub fn parse_inline(&mut self) -> Result<Inline, ParseError> {
unimplemented!()
}
pub fn parse_text(&mut self) -> Result<Text, ParseError> { pub fn parse_text(&mut self) -> Result<Text, ParseError> {
unimplemented!() unimplemented!()
} }

@ -1,3 +1,5 @@
use std::ops::Sub;
pub enum Block { pub enum Block {
Section(Section), Section(Section),
Paragraph(Paragraph), Paragraph(Paragraph),
@ -7,7 +9,6 @@ pub enum Block {
pub enum Inline { pub enum Inline {
Text(Text), Text(Text),
Code(Code),
} }
pub struct Document { pub struct Document {
@ -20,8 +21,8 @@ pub struct Section {
} }
pub struct Header { pub struct Header {
pub(crate) size: u8, pub size: u8,
pub(crate) text: Text, pub line: Inline,
} }
pub struct BlockQuote { pub struct BlockQuote {
@ -29,16 +30,16 @@ pub struct BlockQuote {
} }
pub struct Paragraph { pub struct Paragraph {
elements: Vec<Inline>, pub elements: Vec<Inline>,
} }
pub struct List { pub struct List {
ordered: bool, pub ordered: bool,
items: Vec<ListItem>, pub items: Vec<ListItem>,
} }
pub struct ListItem { pub struct ListItem {
text: Vec<Inline>, text: Inline,
} }
pub struct Table { pub struct Table {
@ -51,7 +52,7 @@ pub struct Row {
} }
pub struct Cell { pub struct Cell {
text: Vec<Inline>, text: Inline,
} }
pub struct CodeBlock { pub struct CodeBlock {
@ -64,13 +65,38 @@ pub struct Code {
} }
pub struct Text { pub struct Text {
bold: bool, subtext: Vec<SubText>,
italic: bool, }
underlined: bool,
striked: bool, pub enum SubText {
Plain(PlainText),
Code(Code),
Bold(BoldText),
Italic(ItalicText),
Underlined(UnderlinedText),
Striked(StrikedText),
}
pub struct PlainText {
value: String, value: String,
} }
pub struct BoldText {
value: Box<SubText>,
}
pub struct ItalicText {
value: Box<SubText>,
}
pub struct UnderlinedText {
value: Box<SubText>,
}
pub struct StrikedText {
value: Box<SubText>,
}
impl Document { impl Document {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
@ -95,3 +121,30 @@ impl Section {
self.elements.push(element) self.elements.push(element)
} }
} }
impl Paragraph {
pub fn new() -> Self {
Self {
elements: Vec::new(),
}
}
pub fn add_element(&mut self, element: Inline) {
self.elements.push(element)
}
}
impl List {
pub fn new() -> Self {
Self {
ordered: false,
items: Vec::new(),
}
}
pub fn add_item(&mut self, item: ListItem) {
self.items.push(item)
}
}
// TODO: Images, URIs

Loading…
Cancel
Save