Skip to content

Commit af24ac4

Browse files
committed
Added support for Flat Open Document Text files (.fodt)
1 parent b1e0436 commit af24ac4

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ fn generate_app_bundle() {
369369
<string>pdf</string>
370370
<string>docx</string>
371371
<string>odt</string>
372+
<string>fodt</string>
372373
<string>pptx</string>
373374
<string>odp</string>
374375
<string>chm</string>

paperback.iss.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
Name: "assoc_html"; Description: "Associate with HTML Documents (*.htm, *.html, *.xhtml)"; Flags: unchecked
3737
Name: "assoc_markdown"; Description: "Associate with Markdown Documents (*.md, *.markdown, *.mdx, *.mdown, *.mdwn, *.mkd, *.mkdn, *.mkdown, *.ronn)"; Flags: unchecked
3838
Name: "assoc_odp"; Description: "Associate with OpenDocument presentations (*.odp)"; Flags: unchecked
39-
Name: "assoc_odt"; Description: "Associate with OpenDocument text files (*.odt)"; Flags: unchecked
39+
Name: "assoc_odt"; Description: "Associate with OpenDocument text files (*.odt, *.fodt)"; Flags: unchecked
4040
Name: "assoc_pdf"; Description: "Associate with PDF Documents (*.pdf)"
4141
Name: "assoc_pptx"; Description: "Associate with PowerPoint Presentations (*.pptx, *.pptm)"; Flags: unchecked
4242
Name: "assoc_rtf"; Description: "Associate with RTF files (*.rtf)"
@@ -79,6 +79,7 @@
7979
Root: HKCR; Subkey: ".odp\OpenWithProgids"; ValueType: string; ValueName: "Paperback.Document"; ValueData: ""; Flags: uninsdeletevalue; Tasks: assoc_odp
8080
; OpenDocument text files.
8181
Root: HKCR; Subkey: ".odt\OpenWithProgids"; ValueType: string; ValueName: "Paperback.Document"; ValueData: ""; Flags: uninsdeletevalue; Tasks: assoc_odt
82+
Root: HKCR; Subkey: ".fodt\OpenWithProgids"; ValueType: string; ValueName: "Paperback.Document"; ValueData: ""; Flags: uninsdeletevalue; Tasks: assoc_odt
8283
; PDF Documents.
8384
Root: HKCR; Subkey: ".pdf\OpenWithProgids"; ValueType: string; ValueName: "Paperback.Document"; ValueData: ""; Flags: uninsdeletevalue; Tasks: assoc_pdf
8485
; PowerPoint Presentations.

src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl ParserRegistry {
9595
registry.register(pdf::PdfParser);
9696
registry.register(markdown::MarkdownParser);
9797
registry.register(odp::OdpParser);
98+
registry.register(odt::FodtParser);
9899
registry.register(odt::OdtParser);
99100
registry.register(pptx::PptxParser);
100101
registry.register(rtf::RtfParser);

src/parser/odt.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, fs::File, io::BufReader};
1+
use std::{collections::HashMap, fs, fs::File, io::BufReader};
22

33
use anyhow::{Context, Result};
44
use roxmltree::{Document as XmlDocument, Node, NodeType};
@@ -51,6 +51,38 @@ impl Parser for OdtParser {
5151
}
5252
}
5353

54+
pub struct FodtParser;
55+
56+
impl Parser for FodtParser {
57+
fn name(&self) -> &'static str {
58+
"Flat OpenDocument Text Files"
59+
}
60+
61+
fn extensions(&self) -> &[&str] {
62+
&["fodt"]
63+
}
64+
65+
fn supported_flags(&self) -> ParserFlags {
66+
ParserFlags::SUPPORTS_TOC
67+
}
68+
69+
fn parse(&self, context: &ParserContext) -> Result<Document> {
70+
let content_str = fs::read_to_string(&context.file_path)
71+
.with_context(|| format!("Failed to open FODT file '{}'", context.file_path))?;
72+
let xml_doc = XmlDocument::parse(&content_str).context("Invalid FODT document")?;
73+
let mut buffer = DocumentBuffer::new();
74+
let mut id_positions = HashMap::new();
75+
traverse(xml_doc.root(), &mut buffer, &mut id_positions);
76+
let title = extract_title_from_path(&context.file_path);
77+
let toc_items = build_toc_from_buffer(&buffer);
78+
let mut document = Document::new().with_title(title);
79+
document.set_buffer(buffer);
80+
document.id_positions = id_positions;
81+
document.toc_items = toc_items;
82+
Ok(document)
83+
}
84+
}
85+
5486
fn traverse(node: Node, buffer: &mut DocumentBuffer, id_positions: &mut HashMap<String, usize>) {
5587
if node.node_type() == NodeType::Element {
5688
let tag_name = node.tag_name().name();

0 commit comments

Comments
 (0)