mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-17 08:05:21 +00:00
Merge pull request #7591 from storytold/dot/pdf-shared-markdown-20261117
Export: use shared extracted-text Markdown writer with exact output parity
This commit is contained in:
4 files changed
+102
-148
No files matched your search
Generated
+66
-6
@@ -1226,11 +1226,26 @@ name = "craft-docpath"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
|
||||
[[package]]
|
||||
name = "craft-flow"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33"
|
||||
dependencies = [
|
||||
"craft-wordmodel",
|
||||
"image",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-fonts-build"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
|
||||
[[package]]
|
||||
name = "craft-html"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33"
|
||||
|
||||
[[package]]
|
||||
name = "craft-i18n"
|
||||
version = "0.1.0"
|
||||
@@ -1294,6 +1309,25 @@ dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-markdown"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33"
|
||||
dependencies = [
|
||||
"craft-flow",
|
||||
"craft-html",
|
||||
"craft-wordmodel",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-math"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33"
|
||||
dependencies = [
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33)",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-menu"
|
||||
version = "0.1.0"
|
||||
@@ -1322,7 +1356,7 @@ name = "craft-opc"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"craft-zip",
|
||||
"log",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1518,6 +1552,20 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-wordmodel"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"craft-math",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
@@ -1527,6 +1575,16 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
@@ -3410,6 +3468,7 @@ dependencies = [
|
||||
"byteorder-lite",
|
||||
"color_quant",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"moxcms",
|
||||
"num-traits",
|
||||
"png 0.18.1",
|
||||
@@ -4759,10 +4818,11 @@ dependencies = [
|
||||
name = "pdfcraft-export"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-markdown",
|
||||
"craft-mime",
|
||||
"craft-opc",
|
||||
"craft-rtf",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"craft-zip",
|
||||
"flate2",
|
||||
]
|
||||
@@ -4914,7 +4974,7 @@ name = "pdfcraft-preflight"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-cms 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8503f24cb845268f47f97e829491114fe9ad1bfa)",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"flate2",
|
||||
"pdfcraft-content",
|
||||
"pdfcraft-cos",
|
||||
@@ -4942,7 +5002,7 @@ name = "pdfcraft-redact"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"pdfcraft-annot",
|
||||
"pdfcraft-content",
|
||||
"pdfcraft-cos",
|
||||
@@ -5110,7 +5170,7 @@ version = "0.21.1"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"craft-image-codecs",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"pdfcraft-cos",
|
||||
"pdfcraft-fonts",
|
||||
"sha2 0.11.1",
|
||||
@@ -5123,7 +5183,7 @@ version = "0.21.1"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"craft-csv",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)",
|
||||
"pdfcraft-annot",
|
||||
"pdfcraft-cos",
|
||||
"pdfcraft-forms",
|
||||
|
||||
@@ -12,6 +12,7 @@ rust-version = "1.95"
|
||||
repository = "https://github.com/storytold/pdfcraft"
|
||||
|
||||
[workspace.dependencies]
|
||||
craft-markdown = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "fbb3f47f7ae712192586984af11ca8f6c0f64d33" }
|
||||
# Small, revision-bound UI drafts; the existing engine remains the owner of PDF edits/history.
|
||||
edit-session = { path = "vendor/edit-session", version = "=0.1.0-alpha.2" }
|
||||
# Shared frontend components; engines remain independent of egui.
|
||||
|
||||
@@ -11,6 +11,7 @@ repository.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-markdown.workspace = true
|
||||
craft-mime.workspace = true
|
||||
craft-opc.workspace = true
|
||||
craft-rtf.workspace = true
|
||||
|
||||
@@ -1,156 +1,42 @@
|
||||
use super::as_html::allowed;
|
||||
use super::layout::{Cell, Item, MAX_COLS, Page, items};
|
||||
//! PDF layout mapping into the shared extracted-text Markdown writer.
|
||||
use super::layout::{Cell, Item, Page, items};
|
||||
use craft_markdown::extracted::{TableCell, Writer};
|
||||
|
||||
/// Markdown's deepest heading.
|
||||
const MAX_HEADING: u8 = 6;
|
||||
#[cfg(test)]
|
||||
pub(crate) use craft_markdown::extracted::{emphasis, paragraph};
|
||||
|
||||
/// `s` as Markdown text on one line: line breaks become spaces, control characters are dropped,
|
||||
/// and every character Markdown would read as markup is escaped with a backslash.
|
||||
fn md_text(s: &str) -> String {
|
||||
let mut o = String::with_capacity(s.len());
|
||||
for c in s.chars().filter(|c| allowed(*c)) {
|
||||
match c {
|
||||
'\n' | '\r' | '\t' => o.push(' '),
|
||||
'\\' | '`' | '*' | '_' | '[' | ']' | '<' | '>' | '#' | '|' | '~' | '!' | '&' => {
|
||||
o.push('\\');
|
||||
o.push(c);
|
||||
}
|
||||
c => o.push(c),
|
||||
}
|
||||
impl TableCell for Cell {
|
||||
fn text(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
o
|
||||
}
|
||||
|
||||
/// `line` with its start escaped where Markdown would read a list item there (`- `, `+ `,
|
||||
/// `1. `, `1) `); the other block markers (`#`, `>`) are escaped by [`md_text`] already.
|
||||
fn block_start(line: String) -> String {
|
||||
let digits = line.bytes().take_while(u8::is_ascii_digit).count();
|
||||
match line.as_bytes().get(digits) {
|
||||
Some(b'.' | b')') if digits > 0 => format!("{}\\{}", line.get(..digits).unwrap_or_default(), line.get(digits..).unwrap_or_default()),
|
||||
Some(b'-' | b'+') if digits == 0 => format!("\\{line}"),
|
||||
_ => line,
|
||||
fn bold(&self) -> bool {
|
||||
self.bold
|
||||
}
|
||||
fn italic(&self) -> bool {
|
||||
self.italic
|
||||
}
|
||||
fn span(&self) -> usize {
|
||||
self.span
|
||||
}
|
||||
}
|
||||
|
||||
/// `text` (already escaped) in bold and/or italic, its outer spaces kept outside. `open` and
|
||||
/// `close` say whether `*` markers would be read as emphasis on that side (see [`marks`]);
|
||||
/// where they would not, as in `foo` + bold `.` + `bar`, the run is wrapped in HTML tags, which
|
||||
/// CommonMark takes inline. The text's own outer spaces count as a side that marks.
|
||||
pub(crate) fn emphasis(text: &str, bold: bool, italic: bool, open: bool, close: bool) -> String {
|
||||
let core = text.trim();
|
||||
if core.is_empty() || !(bold || italic) {
|
||||
return text.to_string();
|
||||
}
|
||||
let lead = text.get(..text.len().saturating_sub(text.trim_start().len())).unwrap_or_default();
|
||||
let trail = text.get(text.trim_end().len()..).unwrap_or_default();
|
||||
let markers = (open || !lead.is_empty()) && (close || !trail.is_empty());
|
||||
let (before, after) = match (markers, bold, italic) {
|
||||
(true, true, true) => ("***", "***"),
|
||||
(true, true, false) => ("**", "**"),
|
||||
(true, false, _) => ("*", "*"),
|
||||
(false, true, true) => ("<strong><em>", "</em></strong>"),
|
||||
(false, true, false) => ("<strong>", "</strong>"),
|
||||
(false, false, _) => ("<em>", "</em>"),
|
||||
};
|
||||
format!("{lead}{before}{core}{after}{trail}")
|
||||
}
|
||||
|
||||
/// A paragraph's runs (text, bold, italic) as one line of Markdown. Neighbouring runs in the
|
||||
/// same style are joined first, so two bold runs never meet as `****`.
|
||||
pub(crate) fn paragraph(runs: &[(&str, bool, bool)]) -> String {
|
||||
let mut merged: Vec<(String, bool, bool)> = Vec::new();
|
||||
for &(text, bold, italic) in runs {
|
||||
let text = md_text(text);
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
match merged.last_mut() {
|
||||
Some(last) if last.1 == bold && last.2 == italic => last.0.push_str(&text),
|
||||
_ => merged.push((text, bold, italic)),
|
||||
}
|
||||
}
|
||||
let mut out = String::new();
|
||||
for (k, (text, bold, italic)) in merged.iter().enumerate() {
|
||||
let core = text.trim();
|
||||
let open = marks(core.chars().next(), out.chars().last());
|
||||
// Markers of a styled neighbour right next to these would join them into one run.
|
||||
let close = match merged.get(k.saturating_add(1)) {
|
||||
Some((next, b, i)) if (*b || *i) && !next.starts_with(char::is_whitespace) => false,
|
||||
next => marks(core.chars().last(), next.and_then(|(n, ..)| n.chars().next())),
|
||||
};
|
||||
out.push_str(&emphasis(text, *bold, *italic, open, close));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether a `*` marker between the text's `edge` character and the `outside` one is read as
|
||||
/// emphasis (CommonMark's flanking rules for `*`): always next to a space or the line's edge,
|
||||
/// never next to another `*`, and otherwise unless the text's edge is punctuation while the
|
||||
/// outside is a letter or digit.
|
||||
fn marks(edge: Option<char>, outside: Option<char>) -> bool {
|
||||
match (edge, outside) {
|
||||
(_, None) => true,
|
||||
(_, Some(o)) if o.is_whitespace() => true,
|
||||
(_, Some('*')) => false,
|
||||
(Some(e), Some(o)) => e.is_alphanumeric() || o.is_ascii_punctuation(),
|
||||
(None, Some(_)) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn cell(c: &Cell) -> String {
|
||||
// A cell sits between `| ` and ` |`, so its markers always have a space outside.
|
||||
emphasis(&md_text(&c.text), c.bold, c.italic, true, true)
|
||||
}
|
||||
|
||||
/// Markdown (CommonMark, with GitHub's pipe tables): headings, paragraphs with bold and italic,
|
||||
/// tables with their first row as the header, and a rule between pages. Images are left out.
|
||||
/// Markdown (CommonMark with pipe tables), retaining PDF reading order and heading detection.
|
||||
/// Image omission is this export's policy; formatting and syntax live in the shared writer.
|
||||
pub fn markdown(pages: &[Page]) -> String {
|
||||
let mut blocks: Vec<String> = Vec::new();
|
||||
for it in items(pages) {
|
||||
match it {
|
||||
Item::Para(runs, lvl) => {
|
||||
// A heading's weight is the heading's own, as in the HTML export.
|
||||
let runs: Vec<(&str, bool, bool)> = runs.iter().map(|r| (r.text.as_str(), r.block.bold && lvl == 0, r.block.italic)).collect();
|
||||
let text = paragraph(&runs);
|
||||
let text = text.trim();
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
blocks.push(match lvl {
|
||||
0 => block_start(text.to_string()),
|
||||
l => format!("{} {text}", "#".repeat(usize::from(l.min(MAX_HEADING)))),
|
||||
});
|
||||
}
|
||||
Item::Table(t) => {
|
||||
// Each row as wide as the widest; a cell spanning columns is followed by empty ones.
|
||||
// Detected tables have at most MAX_COLS columns, and a span never counts for more.
|
||||
let span = |c: &Cell| c.span.clamp(1, MAX_COLS);
|
||||
let width = t.rows.iter().map(|r| r.iter().map(span).fold(0usize, usize::saturating_add)).max().unwrap_or(0);
|
||||
if width == 0 {
|
||||
continue;
|
||||
}
|
||||
let mut lines = Vec::with_capacity(t.rows.len().saturating_add(1));
|
||||
for (i, row) in t.rows.iter().enumerate() {
|
||||
let mut cells: Vec<String> = Vec::with_capacity(width);
|
||||
for c in row {
|
||||
cells.push(cell(c));
|
||||
cells.extend(std::iter::repeat_n(String::new(), span(c).saturating_sub(1)));
|
||||
}
|
||||
cells.resize(width, String::new());
|
||||
lines.push(format!("| {} |", cells.join(" | ")));
|
||||
if i == 0 {
|
||||
lines.push(format!("|{}", " --- |".repeat(width)));
|
||||
}
|
||||
}
|
||||
blocks.push(lines.join("\n"));
|
||||
let mut writer = Writer::new();
|
||||
for item in items(pages) {
|
||||
match item {
|
||||
Item::Para(runs, level) => {
|
||||
// Heading weight comes from the heading marker, as in the HTML export.
|
||||
let runs: Vec<_> = runs.iter().map(|r| (r.text.as_str(), r.block.bold && level == 0, r.block.italic)).collect();
|
||||
writer.paragraph(&runs, level);
|
||||
}
|
||||
Item::Table(table) => writer.table(&table.rows),
|
||||
Item::Img(_) => {}
|
||||
Item::PageBreak => blocks.push("---".into()),
|
||||
Item::PageBreak => writer.page_break(),
|
||||
}
|
||||
}
|
||||
let mut s = blocks.join("\n\n");
|
||||
s.push('\n');
|
||||
s
|
||||
writer.finish()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -176,3 +62,9 @@ mod tests {
|
||||
assert_eq!(paragraph(&[("", true, false), ("\u{1}", false, true), ("\u{7f}", false, false)]), "");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn shared_table_span_limit_matches_layout_contract() {
|
||||
assert_eq!(craft_markdown::extracted::MAX_SPAN, super::layout::MAX_COLS);
|
||||
}
|
||||
Reference in new issue
Block a user