diff options
| author | Philipp A | 2018-11-27 00:45:22 +0100 |
|---|---|---|
| committer | Philipp A | 2018-11-27 00:45:22 +0100 |
| commit | 71d26fbdc4825b4c2a2db125dde388dc5c5a196c (patch) | |
| tree | 15e485c6324bf2a5e6997a0a766345d44d0c4d50 /src/document_tree/extra_attributes.rs | |
| parent | 70667b529fcf24815819636b9a105d17ae38e796 (diff) | |
| download | rust-rst-71d26fbdc4825b4c2a2db125dde388dc5c5a196c.tar.bz2 | |
Support inline Images
Diffstat (limited to 'src/document_tree/extra_attributes.rs')
| -rw-r--r-- | src/document_tree/extra_attributes.rs | 92 |
1 files changed, 52 insertions, 40 deletions
diff --git a/src/document_tree/extra_attributes.rs b/src/document_tree/extra_attributes.rs index 80fe045..916ba41 100644 --- a/src/document_tree/extra_attributes.rs +++ b/src/document_tree/extra_attributes.rs @@ -1,50 +1,27 @@ use url::Url; -use serde::{ - Serialize, - Serializer, - ser::SerializeStruct, -}; +use serde_derive::Serialize; +use super::serde_util::{serialize_url,serialize_opt_url}; use super::attribute_types::{FixedSpace,ID,NameToken,AlignHV,AlignH,Measure,EnumeratedListType}; pub trait ExtraAttributes<A> { -// fn with_extra(extra: A) -> Self; + fn with_extra(extra: A) -> Self; fn extra (& self) -> & A; fn extra_mut(&mut self) -> &mut A; } -macro_rules! count { - () => (0usize); - ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*)); -} - -macro_rules! ser_url { - ($self:ident, refuri ) => { $self.refuri.as_ref().map(|uri| uri.to_string()) }; - ($self:ident, uri ) => { $self.uri.to_string() }; - ($self:ident, $param:ident) => { $self.$param }; -} - macro_rules! impl_extra { - ( $name:ident { $( $param:ident : $type:ty ),* $(,)* } ) => ( + ( $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => ( impl_extra!( - #[derive(Default,Debug)] - $name { $( $param : $type, )* } + #[derive(Default,Debug,Serialize)] + $name { $( $(#[$pattr])* $param : $type, )* } ); ); - ( $(#[$attr:meta])+ $name:ident { $( $param:ident : $type:ty ),* $(,)* } ) => ( + ( $(#[$attr:meta])+ $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => ( $(#[$attr])+ pub struct $name { - $( pub $param : $type, )* - } - - impl Serialize for $name { - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer { - #[allow(unused_mut)] - let mut state = serializer.serialize_struct(stringify!($name), count!($($param)*))?; - $( state.serialize_field(stringify!($param), &ser_url!(self, $param))?; )* - state.end() - } + $( $(#[$pattr])* pub $param : $type, )* } ); } @@ -54,15 +31,24 @@ impl_extra!(LiteralBlock { space: FixedSpace }); impl_extra!(DoctestBlock { space: FixedSpace }); impl_extra!(SubstitutionDefinition { ltrim: Option<bool>, rtrim: Option<bool> }); impl_extra!(Comment { space: FixedSpace }); -impl_extra!(Target { refuri: Option<Url>, refid: Option<ID>, refname: Vec<NameToken>, anonymous: bool }); +impl_extra!(Target { + #[serde(serialize_with = "serialize_opt_url")] + refuri: Option<Url>, + refid: Option<ID>, + refname: Vec<NameToken>, + anonymous: bool, +}); impl_extra!(Raw { space: FixedSpace, format: Vec<NameToken> }); -impl_extra!(#[derive(Debug)] Image { - align: Option<AlignHV>, +impl_extra!(#[derive(Debug,Serialize)] Image { + #[serde(serialize_with = "serialize_url")] uri: Url, + align: Option<AlignHV>, alt: Option<String>, height: Option<Measure>, width: Option<Measure>, - scale: Option<f64>, + scale: Option<u8>, + #[serde(serialize_with = "serialize_opt_url")] + target: Option<Url>, // Not part of the DTD but a valid argument }); //bools usually are XML yesorno. “auto” however either exists and is set to something random like “1” or doesn’t exist @@ -78,20 +64,46 @@ impl_extra!(Table {}); //TODO impl_extra!(OptionArgument { delimiter: Option<String> }); -impl_extra!(Reference { name: Option<String>, refuri: Option<Url>, refid: Option<ID>, refname: Vec<NameToken> }); +impl_extra!(Reference { + name: Option<String>, + #[serde(serialize_with = "serialize_opt_url")] + refuri: Option<Url>, + refid: Option<ID>, + refname: Vec<NameToken>, +}); impl_extra!(FootnoteReference { refid: Option<ID>, refname: Vec<NameToken>, auto: Option<bool> }); impl_extra!(CitationReference { refid: Option<ID>, refname: Vec<NameToken> }); impl_extra!(SubstitutionReference { refname: Vec<NameToken> }); impl_extra!(Problematic { refid: Option<ID> }); //also have non-inline versions. Inline image is no figure child, inline target has content -impl_extra!(TargetInline { refuri: Option<Url>, refid: Option<ID>, refname: Vec<NameToken>, anonymous: bool }); +impl_extra!(TargetInline { + #[serde(serialize_with = "serialize_opt_url")] + refuri: Option<Url>, + refid: Option<ID>, + refname: Vec<NameToken>, + anonymous: bool, +}); impl_extra!(RawInline { space: FixedSpace, format: Vec<NameToken> }); -impl_extra!(#[derive(Debug)] ImageInline { - align: Option<AlignHV>, +impl_extra!(#[derive(Debug,Serialize)] ImageInline { + #[serde(serialize_with = "serialize_url")] uri: Url, + align: Option<AlignHV>, alt: Option<String>, height: Option<Measure>, width: Option<Measure>, - scale: Option<f64>, + scale: Option<u8>, + #[serde(serialize_with = "serialize_opt_url")] + target: Option<Url>, // Not part of the DTD but a valid argument }); +impl ImageInline { + pub fn new(uri: Url) -> ImageInline { ImageInline { + uri: uri, + align: None, + alt: None, + height: None, + width: None, + scale: None, + target: None, + } } +} |
