-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support default section header (#115)
* feat: Add minimum header * feat: Support minimum header feature * fix: fix snaps
- Loading branch information
Showing
57 changed files
with
617 additions
and
107 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use docx_rs::*; | ||
|
||
pub fn main() -> Result<(), DocxError> { | ||
let path = std::path::Path::new("./output/header.docx"); | ||
let file = std::fs::File::create(&path).unwrap(); | ||
Docx::new() | ||
.add_header_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))) | ||
.add_paragraph(Paragraph::new().add_run(Run::new().add_text("World"))) | ||
.build() | ||
.pack(file)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::documents::BuildXML; | ||
use crate::xml_builder::*; | ||
|
||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct HeaderReference { | ||
header_type: String, | ||
id: String, | ||
} | ||
|
||
impl Default for HeaderReference { | ||
fn default() -> HeaderReference { | ||
HeaderReference { | ||
header_type: "default".to_owned(), | ||
id: "rId4".to_owned(), | ||
} | ||
} | ||
} | ||
|
||
impl HeaderReference { | ||
pub fn new(t: impl Into<String>, id: impl Into<String>) -> HeaderReference { | ||
HeaderReference { | ||
header_type: t.into(), | ||
id: id.into(), | ||
} | ||
} | ||
} | ||
|
||
impl BuildXML for HeaderReference { | ||
fn build(&self) -> Vec<u8> { | ||
XMLBuilder::new() | ||
.header_reference(&self.header_type, &self.id) | ||
.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use serde::ser::{SerializeStruct, Serializer}; | ||
use serde::Serialize; | ||
|
||
use super::*; | ||
use crate::documents::BuildXML; | ||
use crate::xml_builder::*; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Header { | ||
pub children: Vec<HeaderChild>, | ||
} | ||
|
||
impl Header { | ||
pub fn new() -> Header { | ||
Default::default() | ||
} | ||
|
||
pub fn add_paragraph(mut self, p: Paragraph) -> Self { | ||
// TODO: support numberings | ||
// if p.has_numbering { | ||
// self.has_numbering = true | ||
// } | ||
self.children.push(HeaderChild::Paragraph(p)); | ||
self | ||
} | ||
|
||
pub fn add_table(mut self, t: Table) -> Self { | ||
// TODO: support numberings | ||
// if t.has_numbering { | ||
// self.has_numbering = true | ||
// } | ||
self.children.push(HeaderChild::Table(t)); | ||
self | ||
} | ||
} | ||
|
||
impl Default for Header { | ||
fn default() -> Self { | ||
Self { children: vec![] } | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum HeaderChild { | ||
Paragraph(Paragraph), | ||
Table(Table), | ||
} | ||
|
||
impl Serialize for HeaderChild { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
match *self { | ||
HeaderChild::Paragraph(ref p) => { | ||
let mut t = serializer.serialize_struct("Paragraph", 2)?; | ||
t.serialize_field("type", "paragraph")?; | ||
t.serialize_field("data", p)?; | ||
t.end() | ||
} | ||
HeaderChild::Table(ref c) => { | ||
let mut t = serializer.serialize_struct("Table", 2)?; | ||
t.serialize_field("type", "table")?; | ||
t.serialize_field("data", c)?; | ||
t.end() | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl BuildXML for Header { | ||
fn build(&self) -> Vec<u8> { | ||
let mut b = XMLBuilder::new(); | ||
b = b.declaration(Some(true)).open_header(); | ||
|
||
for c in &self.children { | ||
match c { | ||
HeaderChild::Paragraph(p) => b = b.add_child(p), | ||
HeaderChild::Table(t) => b = b.add_child(t), | ||
} | ||
} | ||
b.close().build() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
#[cfg(test)] | ||
use pretty_assertions::assert_eq; | ||
use std::str; | ||
|
||
#[test] | ||
fn test_settings() { | ||
let c = Header::new(); | ||
let b = c.build(); | ||
assert_eq!( | ||
str::from_utf8(&b).unwrap(), | ||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<w:hdr xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14" />"# | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
use std::sync::atomic::AtomicUsize; | ||
#[cfg(not(test))] | ||
static HEADER_ID: AtomicUsize = AtomicUsize::new(1); | ||
#[cfg(not(test))] | ||
pub fn generate_header_id() -> usize { | ||
use std::sync::atomic::Ordering; | ||
let id = HEADER_ID.load(Ordering::Relaxed); | ||
HEADER_ID.store(id.wrapping_add(1), Ordering::Relaxed); | ||
id | ||
} | ||
#[cfg(test)] | ||
pub fn generate_header_id() -> usize { | ||
123 | ||
} | ||
pub fn create_header_rid(id: usize) -> String { | ||
format!("rIdImage{}", id) | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.