Skip to content

Commit

Permalink
build: release v0.3.1
Browse files Browse the repository at this point in the history
- fix missing col start event
- pass row start without `|-` (wikitext_tables/11.txt)
  • Loading branch information
p208p2002 committed Jun 12, 2024
1 parent b9c0e27 commit 900dc05
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wikitext_table_parser"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
license = "Apache-2.0"
keywords = ["wiki", "wikitext","table","parser"]
Expand Down
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A table in wikitext should like:
#### Installation
```toml
[dependencies]
wikitext_table_parser = "0.3.0"
wikitext_table_parser = "0.3.1"
```
#### Usage Example
```rust
Expand Down Expand Up @@ -64,25 +64,28 @@ fn main() {
WikitextTableParser::new(table_tokenizer, cell_tokenizer, &content, true);
for event in wikitext_table_parser {
match event {
Event::TableStart => {
Event::TableStart {} => {
println!("Table START!");
}
Event::TableStyle(table_style) => {
Event::TableStyle { text: table_style } => {
println!("table style{:?}#", table_style);
}
Event::TableCaption(text) => {
Event::TableCaption { text } => {
println!("table name{:?}#", text);
}
Event::RowStyle(row_style) => {
Event::RowStyle { text: row_style } => {
println!("----- {:?} -----", row_style);
}
Event::ColStyle(col_style) => {
print!("col style: {:?} -- ", col_style);
Event::ColStart { cell_type } =>{
print!("{:?} ",cell_type);
}
Event::ColEnd(text) => {
println!("col data: {:?}", text);
Event::ColStyle { text: col_style } => {
print!("style: {:?} -> ", col_style);
}
Event::TableEnd => {
Event::ColEnd { text } => {
println!("data: {:?}", text);
}
Event::TableEnd {} => {
println!("Table END!");
}
_ => {}
Expand Down Expand Up @@ -139,6 +142,7 @@ for event in parser.event_log_queue:
print("col style:", event.text)
elif isinstance(event, Event.ColEnd):
print("col data:", event.text)
print("-"*20)
elif isinstance(event, Event.TableCaptionStart):
pass
elif isinstance(event, Event.TableCaption):
Expand All @@ -148,7 +152,7 @@ for event in parser.event_log_queue:
elif isinstance(event, Event.RowStyle):
print("row style:", event.text)
elif isinstance(event, Event.RowEnd):
print("-"*20)
print("="*30)
else:
raise NotImplementedError(event)
```
3 changes: 2 additions & 1 deletion examples/use_in_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
print("col style:", event.text)
elif isinstance(event, Event.ColEnd):
print("col data:", event.text)
print("-"*20)
elif isinstance(event, Event.TableCaptionStart):
pass
elif isinstance(event, Event.TableCaption):
Expand All @@ -43,6 +44,6 @@
elif isinstance(event, Event.RowStyle):
print("row style:", event.text)
elif isinstance(event, Event.RowEnd):
print("-"*20)
print("="*30)
else:
raise NotImplementedError(event)
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ fn main() {
Event::RowStyle { text: row_style } => {
println!("----- {:?} -----", row_style);
}
Event::ColStart { cell_type } =>{
print!("{:?} ",cell_type);
}
Event::ColStyle { text: col_style } => {
print!("col style: {:?} -- ", col_style);
print!("style: {:?} -> ", col_style);
}
Event::ColEnd { text } => {
println!("col data: {:?}", text);
println!("data: {:?}", text);
}
Event::TableEnd {} => {
println!("Table END!");
Expand Down
22 changes: 17 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub enum CellType {
#[derive(Debug)]
pub struct WikitextTableParser {
state: State,
#[pyo3(get,set)]
#[pyo3(get, set)]
event_log_queue: Vec<Event>,
#[pyo3(get,set)]
#[pyo3(get, set)]
tokens: Vec<String>,
text_buffer: String,
table_tokenizer: Tokenizer,
Expand Down Expand Up @@ -165,6 +165,7 @@ impl WikitextTableParser {

fn step(&mut self) {
let token = self.tokens.remove(0);
// println!("{:?}", token);
match self.state {
State::Idle => {
if &token == TableSpecialTokens::TableStart.as_ref() {
Expand All @@ -185,6 +186,12 @@ impl WikitextTableParser {
});
self.clear_text_buffer();
self.transition(Event::RowStart {});
} else if &token == TableSpecialTokens::TableHeaderCell.as_ref() {
self.transition(Event::TableStyle {
text: self.get_text_buffer_data(),
});
self.clear_text_buffer();
self.transition(Event::RowStart {});
}
// end of table
else if &token == TableSpecialTokens::TableEnd.as_ref() {
Expand All @@ -204,6 +211,7 @@ impl WikitextTableParser {
// match ! after the caption, this type will not have a row style
// and should turn in to read col state
else if &token == TableSpecialTokens::TableHeaderCell.as_ref() {
// catch table caption and trans the state to "State::ReadTable"
self.transition(Event::TableCaption {
text: self.get_text_buffer_data(),
});
Expand Down Expand Up @@ -264,6 +272,9 @@ impl WikitextTableParser {
text: self.get_text_buffer_data(),
});
self.clear_text_buffer();
self.transition(Event::ColStart {
cell_type: CellType::DataCell,
});
}
// match \n! or \n!!
else if &token == TableSpecialTokens::TableHeaderCell.as_ref()
Expand All @@ -276,6 +287,9 @@ impl WikitextTableParser {
text: self.get_text_buffer_data(),
});
self.clear_text_buffer();
self.transition(Event::ColStart {
cell_type: CellType::HeaderCell,
});
} else if &token == TableSpecialTokens::TableRow.as_ref() {
self.transition(Event::ColStyle {
text: self.get_style_text_buffer_data(),
Expand Down Expand Up @@ -325,9 +339,7 @@ impl WikitextTableParser {

// State::ReadCol
(State::ReadCol, Event::ColStyle { text }) => {}
(State::ReadCol, Event::ColEnd { text }) => {
self.state = State::ReadCol
},
(State::ReadCol, Event::ColEnd { text }) => self.state = State::ReadCol,
(State::ReadCol, Event::RowStart {}) => self.state = State::ReadRow,

// Else
Expand Down

0 comments on commit 900dc05

Please sign in to comment.