feat: Support bookmark

main
bokuweb 2019-12-04 16:55:03 +09:00
parent 23f9a5de79
commit 9f52bdd6e7
15 changed files with 184 additions and 1 deletions

View File

@ -0,0 +1,39 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct BookmarkEnd<'a> {
id: &'a str,
}
impl<'a> BookmarkEnd<'a> {
pub fn new(id: &'a str) -> BookmarkEnd<'a> {
BookmarkEnd { id }
}
}
impl<'a> BuildXML for BookmarkEnd<'a> {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.bookmark_end(&self.id).build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_bookmark_end() {
let c = BookmarkEnd::new("mockid");
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:bookmarkEnd w:id="mockid" />"#
);
}
}

View File

@ -0,0 +1,40 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct BookmarkStart<'a> {
id: &'a str,
name: &'a str,
}
impl<'a> BookmarkStart<'a> {
pub fn new(id: &'a str, name: &'a str) -> BookmarkStart<'a> {
BookmarkStart { id, name }
}
}
impl<'a> BuildXML for BookmarkStart<'a> {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.bookmark_start(&self.id, &self.name).build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_bookmark_start() {
let c = BookmarkStart::new("mockid", "mockname");
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:bookmarkStart w:id="mockid" w:name="mockname" />"#
);
}
}

View File

@ -1,6 +1,8 @@
mod based_on; mod based_on;
mod bold; mod bold;
mod bold_cs; mod bold_cs;
mod bookmark_end;
mod bookmark_start;
mod br; mod br;
mod color; mod color;
mod default_tab_stop; mod default_tab_stop;
@ -49,6 +51,8 @@ mod zoom;
pub use based_on::*; pub use based_on::*;
pub use bold::*; pub use bold::*;
pub use bold_cs::*; pub use bold_cs::*;
pub use bookmark_end::*;
pub use bookmark_start::*;
pub use br::*; pub use br::*;
pub use color::*; pub use color::*;
pub use default_tab_stop::*; pub use default_tab_stop::*;

View File

@ -1,4 +1,4 @@
use super::{Delete, Insert, ParagraphProperty, Run}; use super::{BookmarkEnd, BookmarkStart, Delete, Insert, ParagraphProperty, Run};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::types::*; use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
@ -25,6 +25,8 @@ pub enum ParagraphChild<'a> {
Run(Run), Run(Run),
Insert(Insert<'a>), Insert(Insert<'a>),
Delete(Delete<'a>), Delete(Delete<'a>),
BookmarkStart(BookmarkStart<'a>),
BookmarkEnd(BookmarkEnd<'a>),
} }
impl<'a> BuildXML for ParagraphChild<'a> { impl<'a> BuildXML for ParagraphChild<'a> {
@ -33,6 +35,8 @@ impl<'a> BuildXML for ParagraphChild<'a> {
ParagraphChild::Run(v) => v.build(), ParagraphChild::Run(v) => v.build(),
ParagraphChild::Insert(v) => v.build(), ParagraphChild::Insert(v) => v.build(),
ParagraphChild::Delete(v) => v.build(), ParagraphChild::Delete(v) => v.build(),
ParagraphChild::BookmarkStart(v) => v.build(),
ParagraphChild::BookmarkEnd(v) => v.build(),
} }
} }
} }
@ -62,6 +66,18 @@ impl<'a> Paragraph<'a> {
self self
} }
pub fn add_bookmark_start(mut self, id: &'a str, name: &'a str) -> Paragraph<'a> {
self.children
.push(ParagraphChild::BookmarkStart(BookmarkStart::new(id, name)));
self
}
pub fn add_bookmark_end(mut self, id: &'a str) -> Paragraph<'a> {
self.children
.push(ParagraphChild::BookmarkEnd(BookmarkEnd::new(id)));
self
}
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph<'a> { pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph<'a> {
self.property = self.property.align(alignment_type); self.property = self.property.align(alignment_type);
self self
@ -128,4 +144,17 @@ mod tests {
r#"<w:p customId="abcd-1234-567890"><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"# r#"<w:p customId="abcd-1234-567890"><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
); );
} }
#[test]
fn test_bookmark() {
let b = Paragraph::new()
.add_bookmark_start("1234-5678", "article")
.add_run(Run::new().add_text("Hello"))
.add_bookmark_end("1234-5678")
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:bookmarkStart w:id="1234-5678" w:name="article" /><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r><w:bookmarkEnd w:id="1234-5678" /></w:p>"#
);
}
} }

View File

@ -167,6 +167,9 @@ impl XMLBuilder {
opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:data"); opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:data");
opened_el!(open_delete, "w:del", "w:id", "w:author", "w:data"); opened_el!(open_delete, "w:del", "w:id", "w:author", "w:data");
closed_el!(bookmark_start, "w:bookmarkStart", "w:id", "w:name");
closed_el!(bookmark_end, "w:bookmarkEnd", "w:id");
} }
#[cfg(test)] #[cfg(test)]

View File

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/><Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/><Override PartName="/word/_rels/document.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/><Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
</Types>

View File

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>

Binary file not shown.

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template></Template><TotalTime>0</TotalTime><Application>LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-3</Application><Pages>1</Pages><Words>3</Words><Characters>18</Characters><CharactersWithSpaces>19</CharactersWithSpaces><Paragraphs>2</Paragraphs></Properties>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dcterms:created xsi:type="dcterms:W3CDTF">2019-12-04T16:33:05Z</dcterms:created><dc:creator></dc:creator><dc:description></dc:description><dc:language>ja-JP</dc:language><cp:lastModifiedBy></cp:lastModifiedBy><dcterms:modified xsi:type="dcterms:W3CDTF">2019-12-04T16:33:39Z</dcterms:modified><cp:revision>1</cp:revision><dc:subject></dc:subject><dc:title></dc:title></cp:coreProperties>

View File

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
</Relationships>

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
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">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr></w:rPr>
</w:pPr>
<w:bookmarkStart w:id="0" w:name="bookmark_test"/>
<w:r>
<w:rPr></w:rPr>
<w:t>Bookmark</w:t>
</w:r>
<w:bookmarkEnd w:id="0"/>
<w:r>
<w:rPr></w:rPr>
<w:t xml:space="preserve"> Hello</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr></w:rPr>
</w:pPr>
<w:r>
<w:rPr></w:rPr>
<w:t>World</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:type w:val="nextPage"/>
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:left="1134" w:right="1134" w:header="0" w:top="1134" w:footer="0" w:bottom="1134" w:gutter="0"/>
<w:pgNumType w:fmt="decimal"/>
<w:formProt w:val="false"/>
<w:textDirection w:val="lrTb"/>
</w:sectPr>
</w:body>
</w:document>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:fonts xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:font w:name="Times New Roman"><w:charset w:val="00"/><w:family w:val="roman"/><w:pitch w:val="variable"/></w:font><w:font w:name="Symbol"><w:charset w:val="02"/><w:family w:val="roman"/><w:pitch w:val="variable"/></w:font><w:font w:name="Arial"><w:charset w:val="00"/><w:family w:val="swiss"/><w:pitch w:val="variable"/></w:font><w:font w:name="Liberation Serif"><w:altName w:val="Times New Roman"/><w:charset w:val="01"/><w:family w:val="roman"/><w:pitch w:val="variable"/></w:font><w:font w:name="Liberation Sans"><w:altName w:val="Arial"/><w:charset w:val="01"/><w:family w:val="swiss"/><w:pitch w:val="variable"/></w:font></w:fonts>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:zoom w:percent="100"/><w:defaultTabStop w:val="709"/><w:compat><w:doNotExpandShiftReturn/></w:compat></w:settings>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14"><w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/><w:kern w:val="2"/><w:sz w:val="24"/><w:szCs w:val="24"/><w:lang w:val="en-US" w:eastAsia="ja-JP" w:bidi="hi-IN"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:widowControl/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal"/><w:qFormat/><w:pPr><w:widowControl/></w:pPr><w:rPr><w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/><w:color w:val="auto"/><w:kern w:val="2"/><w:sz w:val="24"/><w:szCs w:val="24"/><w:lang w:val="en-US" w:eastAsia="ja-JP" w:bidi="hi-IN"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style14"><w:name w:val="見出し"/><w:basedOn w:val="Normal"/><w:next w:val="Style15"/><w:qFormat/><w:pPr><w:keepNext w:val="true"/><w:spacing w:before="240" w:after="120"/></w:pPr><w:rPr><w:rFonts w:ascii="Liberation Sans" w:hAnsi="Liberation Sans" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style15"><w:name w:val="Body Text"/><w:basedOn w:val="Normal"/><w:pPr><w:spacing w:lineRule="auto" w:line="276" w:before="0" w:after="140"/></w:pPr><w:rPr></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style16"><w:name w:val="List"/><w:basedOn w:val="Style15"/><w:pPr></w:pPr><w:rPr><w:rFonts w:cs="Lohit Devanagari"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style17"><w:name w:val="Caption"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:suppressLineNumbers/><w:spacing w:before="120" w:after="120"/></w:pPr><w:rPr><w:rFonts w:cs="Lohit Devanagari"/><w:i/><w:iCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style18"><w:name w:val="索引"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:suppressLineNumbers/></w:pPr><w:rPr><w:rFonts w:cs="Lohit Devanagari"/></w:rPr></w:style></w:styles>