document_tree/
extra_attributes.rs

1use serde_derive::Serialize;
2
3use crate::attribute_types::{
4    AlignH, AlignHV, AlignV, CanBeEmpty, EnumeratedListType, FixedSpace, FootnoteType, ID, Measure,
5    NameToken, TableAlignH, TableBorder, TableGroupCols,
6};
7use crate::elements as e;
8use crate::url::Url;
9
10pub trait ExtraAttributes<A> {
11    fn with_extra(extra: A) -> Self;
12    fn extra(&self) -> &A;
13    fn extra_mut(&mut self) -> &mut A;
14}
15
16macro_rules! impl_extra {
17    ( $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => (
18        impl_extra!(
19            #[derive(Default,Debug,PartialEq,Serialize,Clone)]
20            $name { $( $(#[$pattr])* $param : $type, )* }
21        );
22    );
23    ( $(#[$attr:meta])+ $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => (
24        $(#[$attr])+
25        pub struct $name { $(
26            $(#[$pattr])*
27            #[serde(skip_serializing_if = "CanBeEmpty::is_empty")]
28            pub $param : $type,
29        )* }
30    );
31}
32
33impl_extra!(Address { space: FixedSpace });
34impl_extra!(LiteralBlock { space: FixedSpace });
35impl_extra!(DoctestBlock { space: FixedSpace });
36impl_extra!(SubstitutionDefinition {
37    ltrim: bool,
38    rtrim: bool
39});
40impl_extra!(Comment { space: FixedSpace });
41impl_extra!(Target {
42    /// External reference to a URI/URL
43    refuri: Option<Url>,
44    /// References to ids attributes in other elements
45    refid: Option<ID>,
46    /// Internal reference to the names attribute of another element. May resolve to either an internal or external reference.
47    refname: Vec<NameToken>,
48    anonymous: bool,
49});
50impl_extra!(Raw { space: FixedSpace, format: Vec<NameToken> });
51impl_extra!(#[derive(Debug,PartialEq,Serialize,Clone)] Image {
52    uri: Url,
53    align: Option<AlignHV>,
54    alt: Option<String>,
55    height: Option<Measure>,
56    width: Option<Measure>,
57    scale: Option<u8>,
58    target: Option<Url>,  // Not part of the DTD but a valid argument
59});
60
61//bools usually are XML yesorno. “auto” however either exists and is set to something random like “1” or doesn’t exist
62//does auto actually mean the numbering prefix?
63
64impl_extra!(BulletList { bullet: Option<String> });
65impl_extra!(EnumeratedList { enumtype: Option<EnumeratedListType>, prefix: Option<String>, suffix: Option<String> });
66
67impl_extra!(Footnote { backrefs: Vec<ID>, auto: Option<FootnoteType> });
68impl_extra!(Citation { backrefs: Vec<ID> });
69impl_extra!(SystemMessage { backrefs: Vec<ID>, level: Option<usize>, line: Option<usize>, type_: Option<NameToken> });
70impl_extra!(Figure { align: Option<AlignH>, width: Option<usize> });
71impl_extra!(Table { frame: Option<TableBorder>, colsep: Option<bool>, rowsep: Option<bool>, pgwide: Option<bool> });
72
73impl_extra!(TableGroup { cols: TableGroupCols, colsep: Option<bool>, rowsep: Option<bool>, align: Option<TableAlignH> });
74impl_extra!(TableHead { valign: Option<AlignV> });
75impl_extra!(TableBody { valign: Option<AlignV> });
76impl_extra!(TableRow { rowsep: Option<bool>, valign: Option<AlignV> });
77impl_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> });
78impl_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> });
79
80impl_extra!(OptionArgument { delimiter: Option<String> });
81
82impl_extra!(Reference {
83    name: Option<NameToken>,  //TODO: is CDATA in the DTD, so maybe no nametoken?
84    /// External reference to a URI/URL
85    refuri: Option<Url>,
86    /// References to ids attributes in other elements
87    refid: Option<ID>,
88    /// Internal reference to the names attribute of another element
89    refname: Vec<NameToken>,
90});
91impl_extra!(FootnoteReference { refid: Option<ID>, refname: Vec<NameToken>, auto: Option<FootnoteType> });
92impl_extra!(CitationReference { refid: Option<ID>, refname: Vec<NameToken> });
93impl_extra!(SubstitutionReference { refname: Vec<NameToken> });
94impl_extra!(Problematic { refid: Option<ID> });
95
96//also have non-inline versions. Inline image is no figure child, inline target has content
97impl_extra!(TargetInline {
98    /// External reference to a URI/URL
99    refuri: Option<Url>,
100    /// References to ids attributes in other elements
101    refid: Option<ID>,
102    /// Internal reference to the names attribute of another element. May resolve to either an internal or external reference.
103    refname: Vec<NameToken>,
104    anonymous: bool,
105});
106impl_extra!(RawInline { space: FixedSpace, format: Vec<NameToken> });
107pub type ImageInline = Image;
108
109pub trait FootnoteTypeExt {
110    /// Is this an auto-numbered footnote?
111    fn is_auto(&self) -> bool;
112    /// Is this a symbolic footnote and not a numeric one?
113    fn is_symbol(&self) -> bool;
114    /// The footnote type independent of whether the footnote is auto-numbered.
115    fn footnote_type(&self) -> FootnoteType;
116}
117
118impl FootnoteTypeExt for Option<FootnoteType> {
119    fn is_auto(&self) -> bool {
120        self.is_some()
121    }
122    fn is_symbol(&self) -> bool {
123        matches!(self, Some(FootnoteType::Symbol))
124    }
125    fn footnote_type(&self) -> FootnoteType {
126        // Explicitly numbered and auto-numbered footnotes are numbered
127        self.unwrap_or(FootnoteType::Number)
128    }
129}
130
131impl FootnoteTypeExt for e::Footnote {
132    fn is_auto(&self) -> bool {
133        self.extra().auto.is_auto()
134    }
135    fn is_symbol(&self) -> bool {
136        self.extra().auto.is_symbol()
137    }
138    fn footnote_type(&self) -> FootnoteType {
139        self.extra().auto.footnote_type()
140    }
141}
142
143impl FootnoteTypeExt for e::FootnoteReference {
144    fn is_auto(&self) -> bool {
145        self.extra().auto.is_auto()
146    }
147    fn is_symbol(&self) -> bool {
148        self.extra().auto.is_symbol()
149    }
150    fn footnote_type(&self) -> FootnoteType {
151        self.extra().auto.footnote_type()
152    }
153}
154
155impl Image {
156    #[must_use]
157    pub fn new(uri: Url) -> Image {
158        Image {
159            uri,
160            align: None,
161            alt: None,
162            height: None,
163            width: None,
164            scale: None,
165            target: None,
166        }
167    }
168}