Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ marker to_marker(const FfiMarker& ffi_marker) {
to_wxstring(ffi_marker.text),
to_wxstring(ffi_marker.reference),
ffi_marker.level,
ffi_marker.length,
};
}

Expand Down
1 change: 1 addition & 0 deletions app/document_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct marker {
wxString text;
wxString ref;
int level;
size_t length{0};
};

struct toc_item {
Expand Down
3 changes: 2 additions & 1 deletion app/document_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ marker to_marker(const FfiMarker& ffi_marker) {
to_wxstring(ffi_marker.text),
to_wxstring(ffi_marker.reference),
ffi_marker.level,
ffi_marker.length,
};
}

Expand Down Expand Up @@ -1380,7 +1381,7 @@ void document_manager::activate_current_table() {
if (table_index == -1) return;
const auto table_marker = doc_get_marker(*doc, table_index);
if (!table_marker.has_value()) return;
if (static_cast<size_t>(current_pos) < table_marker->pos || static_cast<size_t>(current_pos) > (table_marker->pos + table_marker->text.length())) return;
if (static_cast<size_t>(current_pos) < table_marker->pos || static_cast<size_t>(current_pos) > (table_marker->pos + table_marker->length)) return;
table_dialog dlg(&main_win, _("Table"), table_marker->ref);
dlg.ShowModal();
}
5 changes: 4 additions & 1 deletion lib/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub mod ffi {
pub text: String,
pub reference: String,
pub level: i32,
pub length: usize,
}

pub struct FfiMarkerResult {
Expand Down Expand Up @@ -704,6 +705,7 @@ fn parse_document(file_path: &str, password: &str) -> Result<ffi::FfiDocument, S
text: m.text,
reference: m.reference,
level: m.level,
length: m.length,
})
.collect(),
toc_items,
Expand Down Expand Up @@ -784,11 +786,12 @@ fn document_marker_to_ffi(marker: &crate::document::Marker) -> ffi::FfiMarker {
text: marker.text.clone(),
reference: marker.reference.clone(),
level: marker.level,
length: marker.length,
}
}

const fn empty_ffi_marker() -> ffi::FfiMarker {
ffi::FfiMarker { marker_type: -1, position: 0, text: String::new(), reference: String::new(), level: 0 }
ffi::FfiMarker { marker_type: -1, position: 0, text: String::new(), reference: String::new(), level: 0, length: 0 }
}

const fn document_stats_to_ffi(stats: &crate::document::DocumentStats) -> ffi::FfiDocumentStats {
Expand Down
9 changes: 8 additions & 1 deletion lib/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ pub struct Marker {
pub text: String,
pub reference: String,
pub level: i32,
pub length: usize,
}

impl Marker {
#[must_use]
pub const fn new(marker_type: MarkerType, position: usize) -> Self {
Self { marker_type, position, text: String::new(), reference: String::new(), level: 0 }
Self { marker_type, position, text: String::new(), reference: String::new(), level: 0, length: 0 }
}

#[must_use]
Expand All @@ -83,6 +84,12 @@ impl Marker {
self.level = level;
self
}

#[must_use]
pub const fn with_length(mut self, length: usize) -> Self {
self.length = length;
self
}
}

#[derive(Debug, Clone)]
Expand Down
43 changes: 42 additions & 1 deletion lib/src/html_to_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct TableInfo {
pub offset: usize,
pub text: String,
pub html_content: String,
pub length: usize,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -234,6 +235,38 @@ impl HtmlToText {
let table_html = Self::serialize_node(node, document);
let start_lines_count = self.lines.len();
let start_offset = self.get_current_text_position();
let mut table_caption = String::new();
for child in node.children() {
if let Node::Element(element) = child.value() {
if element.name() == "caption" {
table_caption = Self::collect_text(child).trim().to_string();
break;
}
}
}
if table_caption.is_empty() {
for child in node.children() {
if let Node::Element(element) = child.value() {
let name = element.name();
if name == "tr" {
table_caption = Self::collect_text(child).trim().to_string();
break;
} else if matches!(name, "thead" | "tbody" | "tfoot") {
for subchild in child.children() {
if let Node::Element(subelem) = subchild.value() {
if subelem.name() == "tr" {
table_caption = Self::collect_text(subchild).trim().to_string();
break;
}
}
}
if !table_caption.is_empty() {
break;
}
}
}
}
}
for child in node.children() {
self.process_node(child, document);
}
Expand All @@ -250,7 +283,15 @@ impl HtmlToText {
self.current_line.push_str(&table_text);
self.finalize_current_line();
}
self.tables.push(TableInfo { offset: start_offset, text: table_text, html_content: table_html });
if table_caption.trim().is_empty() {
table_caption = "table".to_string();
}
self.tables.push(TableInfo {
offset: start_offset,
text: table_caption,
html_content: table_html,
length: table_text.len(),
});
}

fn handle_element_opening(&mut self, tag_name: &str, node: NodeRef<'_, Node>, document: &Html) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/parser/chm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ impl Parser for ChmParser {
buffer.add_marker(
Marker::new(MarkerType::Table, section_start + table.offset)
.with_text(table.text.clone())
.with_reference(table.html_content.clone()),
.with_reference(table.html_content.clone())
.with_length(table.length),
);
}
if !buffer.content.ends_with('\n') {
Expand Down
15 changes: 9 additions & 6 deletions lib/src/parser/docx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn traverse(
fn process_table(element: Node, buffer: &mut DocumentBuffer, _rels: &HashMap<String, String>) {
let table_start = buffer.current_position();
let mut html_content = String::from("<table border=\"1\">");
let mut placeholder_text = String::from("table: ");
let mut table_caption = String::from("table: ");
let mut first_row = true;
for child in element.children() {
if child.node_type() == NodeType::Element && child.tag_name().name() == "tr" {
Expand All @@ -104,8 +104,8 @@ fn process_table(element: Node, buffer: &mut DocumentBuffer, _rels: &HashMap<Str
html_content.push_str(trimmed_cell);
html_content.push_str("</td>");
if first_row {
placeholder_text.push_str(trimmed_cell);
placeholder_text.push(' ');
table_caption.push_str(trimmed_cell);
table_caption.push(' ');
}
}
}
Expand All @@ -114,11 +114,14 @@ fn process_table(element: Node, buffer: &mut DocumentBuffer, _rels: &HashMap<Str
}
}
html_content.push_str("</table>");
let final_placeholder = placeholder_text.trim().to_string();
buffer.append(&final_placeholder);
let final_caption = table_caption.trim().to_string();
buffer.append(&final_caption);
buffer.append("\n");
buffer.add_marker(
Marker::new(MarkerType::Table, table_start).with_text(final_placeholder).with_reference(html_content),
Marker::new(MarkerType::Table, table_start)
.with_text(final_caption.clone())
.with_reference(html_content)
.with_length(final_caption.len()),
);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/parser/epub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ impl Parser for EpubParser {
buffer.add_marker(
Marker::new(MarkerType::Table, section_start + table.offset)
.with_text(table.text)
.with_reference(table.html_content),
.with_reference(table.html_content)
.with_length(table.length),
);
}
for list in section.lists {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/parser/fb2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ impl Parser for Fb2Parser {
buffer.add_marker(
Marker::new(MarkerType::Table, table.offset)
.with_text(table.text.clone())
.with_reference(table.html_content.clone()),
.with_reference(table.html_content.clone())
.with_length(table.length),
);
}
for link in converter.get_links() {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/parser/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl Parser for HtmlParser {
buffer.add_marker(
Marker::new(MarkerType::Table, table.offset)
.with_text(table.text.clone())
.with_reference(table.html_content.clone()),
.with_reference(table.html_content.clone())
.with_length(table.length),
);
}
let toc_items = build_toc_from_headings(converter.get_headings());
Expand Down
3 changes: 2 additions & 1 deletion lib/src/parser/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ impl Parser for MarkdownParser {
buffer.add_marker(
Marker::new(MarkerType::Table, table.offset)
.with_text(table.text.clone())
.with_reference(table.html_content.clone()),
.with_reference(table.html_content.clone())
.with_length(table.length),
);
}
for list in converter.get_lists() {
Expand Down
21 changes: 19 additions & 2 deletions lib/src/parser/odt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,20 @@ fn traverse_children(node: Node, buffer: &mut DocumentBuffer, id_positions: &mut
fn process_table(node: Node, buffer: &mut DocumentBuffer, id_positions: &mut HashMap<String, usize>) {
let table_start = buffer.current_position();
let mut html_content = String::from("<table border=\"1\">");
let mut table_caption = String::new();
let mut found_first_row = false;
for child in node.children() {
if child.is_element() && child.tag_name().name() == "table-row" {
if !found_first_row {
for cell in child.children() {
if cell.is_element() && cell.tag_name().name() == "table-cell" {
let cell_text = collect_element_text(cell);
table_caption.push_str(&cell_text);
table_caption.push(' ');
}
}
found_first_row = true;
}
html_content.push_str("<tr>");
for cell in child.children() {
if cell.is_element() && cell.tag_name().name() == "table-cell" {
Expand All @@ -132,7 +144,12 @@ fn process_table(node: Node, buffer: &mut DocumentBuffer, id_positions: &mut Has
let table_end = buffer.current_position();
let table_text = buffer.content[table_start..table_end].to_string();
if !table_text.trim().is_empty() {
buffer
.add_marker(Marker::new(MarkerType::Table, table_start).with_text(table_text).with_reference(html_content));
let marker_text = if !table_caption.trim().is_empty() { table_caption } else { "table".to_string() };
buffer.add_marker(
Marker::new(MarkerType::Table, table_start)
.with_text(marker_text)
.with_reference(html_content)
.with_length(table_text.len()),
);
}
}
38 changes: 37 additions & 1 deletion lib/src/xml_to_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,34 @@ impl XmlToText {
let table_xml = node.document().input_text()[node.range()].to_string();
let start_lines_count = self.lines.len();
let start_offset = self.get_current_text_position();
let mut table_caption = String::new();
for child in node.children() {
if child.is_element() && child.tag_name().name() == "caption" {
table_caption = collect_element_text(child).trim().to_string();
break;
}
}
if table_caption.is_empty() {
for child in node.children() {
if child.is_element() {
let name = child.tag_name().name();
if name == "tr" {
table_caption = collect_element_text(child).trim().to_string();
break;
} else if matches!(name, "thead" | "tbody" | "tfoot") {
for subchild in child.children() {
if subchild.is_element() && subchild.tag_name().name() == "tr" {
table_caption = collect_element_text(subchild).trim().to_string();
break;
}
}
if !table_caption.is_empty() {
break;
}
}
}
}
}
for child in node.children() {
self.process_node(child);
}
Expand All @@ -204,7 +232,15 @@ impl XmlToText {
self.current_line.push_str(&table_text);
self.finalize_current_line();
}
self.tables.push(TableInfo { offset: start_offset, text: table_text, html_content: table_xml });
if table_caption.trim().is_empty() {
table_caption = "table".to_string();
}
self.tables.push(TableInfo {
offset: start_offset,
text: table_caption,
html_content: table_xml,
length: table_text.len(),
});
}

fn handle_list_item_xml(&mut self, node: Node<'_, '_>) {
Expand Down