document_tree/
extra_attributes.rs

1use serde_derive::Serialize;
2
3use crate::attribute_types::{
4    AlignH, AlignHV, AlignV, AutoFootnoteType, CanBeEmpty, EnumeratedListType, FixedSpace, ID,
5    Measure, NameToken, TableAlignH, TableBorder, TableGroupCols,
6};
7use crate::url::Url;
8
9pub trait ExtraAttributes<A> {
10    fn with_extra(extra: A) -> Self;
11    fn extra(&self) -> &A;
12    fn extra_mut(&mut self) -> &mut A;
13}
14
15macro_rules! impl_extra {
16    ( $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => (
17        impl_extra!(
18            #[derive(Default,Debug,PartialEq,Serialize,Clone)]
19            $name { $( $(#[$pattr])* $param : $type, )* }
20        );
21    );
22    ( $(#[$attr:meta])+ $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => (
23        $(#[$attr])+
24        pub struct $name { $(
25            $(#[$pattr])*
26            #[serde(skip_serializing_if = "CanBeEmpty::is_empty")]
27            pub $param : $type,
28        )* }
29    );
30}
31
32impl_extra!(Address { space: FixedSpace });
33impl_extra!(LiteralBlock { space: FixedSpace });
34impl_extra!(DoctestBlock { space: FixedSpace });
35impl_extra!(SubstitutionDefinition {
36    ltrim: bool,
37    rtrim: bool
38});
39impl_extra!(Comment { space: FixedSpace });
40impl_extra!(Target {
41    /// External reference to a URI/URL
42    refuri: Option<Url>,
43    /// References to ids attributes in other elements
44    refid: Option<ID>,
45    /// Internal reference to the names attribute of another element. May resolve to either an internal or external reference.
46    refname: Vec<NameToken>,
47    anonymous: bool,
48});
49impl_extra!(Raw { space: FixedSpace, format: Vec<NameToken> });
50impl_extra!(#[derive(Debug,PartialEq,Serialize,Clone)] Image {
51    uri: Url,
52    align: Option<AlignHV>,
53    alt: Option<String>,
54    height: Option<Measure>,
55    width: Option<Measure>,
56    scale: Option<u8>,
57    target: Option<Url>,  // Not part of the DTD but a valid argument
58});
59
60//bools usually are XML yesorno. “auto” however either exists and is set to something random like “1” or doesn’t exist
61//does auto actually mean the numbering prefix?
62
63impl_extra!(BulletList { bullet: Option<String> });
64impl_extra!(EnumeratedList { enumtype: Option<EnumeratedListType>, prefix: Option<String>, suffix: Option<String> });
65
66impl_extra!(Footnote { backrefs: Vec<ID>, auto: Option<AutoFootnoteType> });
67impl_extra!(Citation { backrefs: Vec<ID> });
68impl_extra!(SystemMessage { backrefs: Vec<ID>, level: Option<usize>, line: Option<usize>, type_: Option<NameToken> });
69impl_extra!(Figure { align: Option<AlignH>, width: Option<usize> });
70impl_extra!(Table { frame: Option<TableBorder>, colsep: Option<bool>, rowsep: Option<bool>, pgwide: Option<bool> });
71
72impl_extra!(TableGroup { cols: TableGroupCols, colsep: Option<bool>, rowsep: Option<bool>, align: Option<TableAlignH> });
73impl_extra!(TableHead { valign: Option<AlignV> });
74impl_extra!(TableBody { valign: Option<AlignV> });
75impl_extra!(TableRow { rowsep: Option<bool>, valign: Option<AlignV> });
76impl_extra!(TableEntry { colname: Option<NameToken>, namest: Option<NameToken>, nameend: Option<NameToken>, morerows: Option<usize>, colsep: Option<bool>, rowsep: Option<bool>, align: Option<TableAlignH>, r#char: Option<char>, charoff: Option<usize>, valign: Option<AlignV>, morecols: Option<usize> });
77impl_extra!(TableColspec { colnum: Option<usize>, colname: Option<NameToken>, colwidth: Option<String>, colsep: Option<bool>, rowsep: Option<bool>, align: Option<TableAlignH>, r#char: Option<char>, charoff: Option<usize>, stub: Option<bool> });
78
79impl_extra!(OptionArgument { delimiter: Option<String> });
80
81impl_extra!(Reference {
82    name: Option<NameToken>,  //TODO: is CDATA in the DTD, so maybe no nametoken?
83    /// External reference to a URI/URL
84    refuri: Option<Url>,
85    /// References to ids attributes in other elements
86    refid: Option<ID>,
87    /// Internal reference to the names attribute of another element
88    refname: Vec<NameToken>,
89});
90impl_extra!(FootnoteReference { refid: Option<ID>, refname: Vec<NameToken>, auto: bool });
91impl_extra!(CitationReference { refid: Option<ID>, refname: Vec<NameToken> });
92impl_extra!(SubstitutionReference { refname: Vec<NameToken> });
93impl_extra!(Problematic { refid: Option<ID> });
94
95//also have non-inline versions. Inline image is no figure child, inline target has content
96impl_extra!(TargetInline {
97    /// External reference to a URI/URL
98    refuri: Option<Url>,
99    /// References to ids attributes in other elements
100    refid: Option<ID>,
101    /// Internal reference to the names attribute of another element. May resolve to either an internal or external reference.
102    refname: Vec<NameToken>,
103    anonymous: bool,
104});
105impl_extra!(RawInline { space: FixedSpace, format: Vec<NameToken> });
106pub type ImageInline = Image;
107
108impl Image {
109    #[must_use]
110    pub fn new(uri: Url) -> Image {
111        Image {
112            uri,
113            align: None,
114            alt: None,
115            height: None,
116            width: None,
117            scale: None,
118            target: None,
119        }
120    }
121}