feat: style reader (#36)

main
bokuweb 2020-02-12 15:44:53 +09:00 committed by GitHub
parent 1b944aeb97
commit 6ec9be7fcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 361 additions and 31 deletions

View File

@ -1,6 +1,7 @@
use serde::Serialize;
use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*;
use crate::StyleType;
@ -32,31 +33,35 @@ impl Default for Style {
}
impl Style {
pub fn new(
style_id: impl Into<String>,
name: impl Into<String>,
style_type: StyleType,
) -> Self {
let name = Name::new(name.into());
pub fn new(style_id: impl Into<String>, style_type: StyleType) -> Self {
let default = Default::default();
Style {
style_id: style_id.into(),
style_type,
name,
..default
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Name::new(name);
self
}
pub fn size(mut self, size: usize) -> Self {
self.run_property = self.run_property.size(size);
self
}
pub fn color(mut self, color: &str) -> Self {
pub fn color(mut self, color: impl Into<String>) -> Self {
self.run_property = self.run_property.color(color);
self
}
pub fn highlight(mut self, color: impl Into<String>) -> Self {
self.run_property = self.run_property.highlight(color);
self
}
pub fn bold(mut self) -> Self {
self.run_property = self.run_property.bold();
self
@ -66,6 +71,31 @@ impl Style {
self.run_property = self.run_property.italic();
self
}
pub fn underline(mut self, line_type: impl Into<String>) -> Self {
self.run_property = self.run_property.underline(line_type);
self
}
pub fn vanish(mut self) -> Self {
self.run_property = self.run_property.vanish();
self
}
pub fn align(mut self, alignment_type: AlignmentType) -> Self {
self.paragraph_property = self.paragraph_property.align(alignment_type);
self
}
pub fn indent(
mut self,
left: usize,
special_indent: Option<SpecialIndentType>,
end: Option<usize>,
) -> Self {
self.paragraph_property = self.paragraph_property.indent(left, special_indent, end);
self
}
}
impl BuildXML for Style {
@ -94,7 +124,7 @@ mod tests {
#[test]
fn test_build() {
let c = Style::new("Heading", "Heading1", StyleType::Paragraph);
let c = Style::new("Heading", StyleType::Paragraph).name("Heading1");
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),

View File

@ -35,7 +35,7 @@ impl Default for Styles {
impl BuildXML for Styles {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let normal = Style::new("Normal", "Normal", StyleType::Paragraph);
let normal = Style::new("Normal", StyleType::Paragraph).name("Normal");
b.open_styles()
.add_child(&self.doc_defaults)
.add_child(&normal)
@ -56,7 +56,8 @@ mod tests {
#[test]
fn test_style() {
let c = Styles::new().add_style(Style::new("Title", "TitleName", StyleType::Paragraph));
let c =
Styles::new().add_style(Style::new("Title", StyleType::Paragraph).name("TitleName"));
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),

View File

@ -0,0 +1,30 @@
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use crate::types::*;
use super::super::errors::*;
pub fn read_indent(
attrs: &[OwnedAttribute],
) -> Result<(usize, Option<usize>, Option<SpecialIndentType>), ReaderError> {
let mut start = 0;
let mut end: Option<usize> = None;
let mut special: Option<SpecialIndentType> = None;
for a in attrs {
let local_name = &a.name.local_name;
if local_name == "left" || local_name == "start" {
start = usize::from_str(&a.value)?;
} else if local_name == "end" || local_name == "right" {
end = Some(usize::from_str(&a.value)?);
} else if local_name == "hanging" {
special = Some(SpecialIndentType::Hanging(usize::from_str(&a.value)?))
} else if local_name == "firstLine" {
special = Some(SpecialIndentType::FirstLine(usize::from_str(&a.value)?))
}
}
Ok((start, end, special))
}

View File

@ -1,3 +1,5 @@
mod indent;
mod width;
pub use indent::*;
pub use width::*;

View File

@ -8,6 +8,8 @@ mod numbering_property;
mod paragraph;
mod rels;
mod run;
mod style;
mod styles;
mod table;
mod table_cell;
mod table_row;

View File

@ -6,6 +6,7 @@ use xml::reader::{EventReader, XmlEvent};
use super::*;
use super::attributes::*;
use crate::types::*;
impl ElementReader for Paragraph {
@ -79,25 +80,7 @@ impl ElementReader for Paragraph {
continue;
}
XMLElement::Indent => {
let mut start = 0;
let mut end: Option<usize> = None;
let mut special: Option<SpecialIndentType> = None;
for a in attributes {
let local_name = &a.name.local_name;
if local_name == "left" || local_name == "start" {
start = usize::from_str(&a.value)?;
} else if local_name == "end" || local_name == "right" {
end = Some(usize::from_str(&a.value)?);
} else if local_name == "hanging" {
special =
Some(SpecialIndentType::Hanging(usize::from_str(&a.value)?))
} else if local_name == "firstLine" {
special = Some(SpecialIndentType::FirstLine(usize::from_str(
&a.value,
)?))
}
}
let (start, end, special) = read_indent(&attributes)?;
p = p.indent(start, special, end);
continue;
}

View File

@ -0,0 +1,77 @@
use std::io::Read;
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};
use super::*;
use crate::types::*;
impl ElementReader for Style {
fn read<R: Read>(
r: &mut EventReader<R>,
attrs: &[OwnedAttribute],
) -> Result<Self, ReaderError> {
let mut id = "".to_owned();
let mut style_type = StyleType::Paragraph;
for a in attrs {
let local_name = &a.name.local_name;
if local_name == "styleId" {
id = a.value.clone();
} else if local_name == "type" {
style_type = StyleType::from_str(&a.value)?;
}
}
let mut style = Style::new(id, style_type);
loop {
let e = r.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::Name => {
style = style.name(&attributes[0].value.clone());
continue;
}
// pPr
XMLElement::Indent => {
let (start, end, special) = read_indent(&attributes)?;
style = style.indent(start, special, end);
continue;
}
XMLElement::Justification => {
style = style.align(AlignmentType::from_str(&attributes[0].value)?);
continue;
}
// rPr
XMLElement::Bold => style = style.bold(),
XMLElement::Highlight => {
style = style.highlight(attributes[0].value.clone())
}
XMLElement::Color => style = style.color(attributes[0].value.clone()),
XMLElement::Size => {
style = style.size(usize::from_str(&attributes[0].value)?)
}
XMLElement::Underline => {
style = style.underline(&attributes[0].value.clone())
}
XMLElement::Italic => style = style.italic(),
XMLElement::Vanish => style = style.vanish(),
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Style = e {
return Ok(style);
}
}
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
}
}
}
}

View File

@ -0,0 +1,64 @@
use std::io::Read;
use xml::reader::{EventReader, XmlEvent};
use super::*;
use crate::reader::{FromXML, ReaderError};
use std::str::FromStr;
impl FromXML for Styles {
fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
let mut parser = EventReader::new(reader);
let mut styles = Self::default();
loop {
let e = parser.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Style = e {
let s = Style::read(&mut parser, &attributes)?;
styles = styles.add_style(s);
continue;
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Styles = e {
break;
}
}
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
}
}
Ok(styles)
}
}
// #[cfg(test)]
// mod tests {
//
// use super::*;
// #[cfg(test)]
// use pretty_assertions::assert_eq;
//
// #[test]
// fn test_from_xml() {
// let xml = r#"<?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" />
// </Relationships>"#;
// let c = Rels::from_xml(xml.as_bytes()).unwrap();
// let mut rels = Vec::new();
// rels.push((
// "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
// .to_owned(),
// "rId1".to_owned(),
// "docProps/core.xml".to_owned(),
// ));
// assert_eq!(Rels { rels }, c);
// }
// }
//

View File

@ -27,6 +27,7 @@ pub enum XMLElement {
Tab,
ParagraphStyle,
Indent,
Name,
Alignment,
NumberingProperty,
IndentLevel,
@ -58,6 +59,12 @@ pub enum XMLElement {
TableCellMargin,
TableGrid,
GridCol,
Style,
BasedOn,
Next,
VertAlign,
Spacing,
Styles,
Unsupported,
}
@ -82,6 +89,7 @@ impl FromStr for XMLElement {
"iCs" => Ok(XMLElement::ItalicCs),
"vanish" => Ok(XMLElement::Vanish),
"italic" => Ok(XMLElement::Italic),
"name" => Ok(XMLElement::Name),
"tab" => Ok(XMLElement::Tab),
"br" => Ok(XMLElement::Break),
"ind" => Ok(XMLElement::Indent),
@ -115,6 +123,12 @@ impl FromStr for XMLElement {
"tblCellMar" => Ok(XMLElement::TableCellMargin),
"tblGrid" => Ok(XMLElement::TableGrid),
"gridCol" => Ok(XMLElement::GridCol),
"style" => Ok(XMLElement::Style),
"basedOn" => Ok(XMLElement::BasedOn),
"next" => Ok(XMLElement::Next),
"vertAlign" => Ok(XMLElement::VertAlign),
"spacing" => Ok(XMLElement::Spacing),
"styles" => Ok(XMLElement::Styles),
_ => Ok(XMLElement::Unsupported),
}
}

View File

@ -1,7 +1,9 @@
use std::fmt;
use wasm_bindgen::prelude::*;
use super::errors;
use serde::Serialize;
use std::str::FromStr;
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Serialize, Copy)]
@ -19,3 +21,14 @@ impl fmt::Display for StyleType {
}
}
}
impl FromStr for StyleType {
type Err = errors::TypeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"paragraph" => Ok(StyleType::Paragraph),
"character" => Ok(StyleType::Character),
_ => Err(errors::TypeError::FromStrError),
}
}
}

View File

@ -1,2 +1,116 @@
<?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:style w:type="paragraph" w:styleId="Style19"><w:name w:val="表の内容"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:suppressLineNumbers/></w:pPr><w:rPr></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style20"><w:name w:val="表の見出し"/><w:basedOn w:val="Style19"/><w:qFormat/><w:pPr><w:suppressLineNumbers/><w:jc w:val="center"/></w:pPr><w:rPr><w:b/><w:bCs/></w:rPr></w:style></w:styles>
<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:style w:type="paragraph" w:styleId="Style19">
<w:name w:val="表の内容"/>
<w:basedOn w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:suppressLineNumbers/>
</w:pPr>
<w:rPr></w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style20">
<w:name w:val="表の見出し"/>
<w:basedOn w:val="Style19"/>
<w:qFormat/>
<w:pPr>
<w:suppressLineNumbers/>
<w:jc w:val="center"/>
</w:pPr>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:style>
</w:styles>