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::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<AutoFootnoteType> });
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<AutoFootnoteType> });
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 FootnoteType {
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}
115
116impl FootnoteType for Option<AutoFootnoteType> {
117    fn is_auto(&self) -> bool {
118        self.is_some()
119    }
120    fn is_symbol(&self) -> bool {
121        matches!(self, Some(AutoFootnoteType::Symbol))
122    }
123}
124
125impl FootnoteType for e::Footnote {
126    fn is_auto(&self) -> bool {
127        self.extra().auto.is_auto()
128    }
129    fn is_symbol(&self) -> bool {
130        self.extra().auto.is_symbol()
131    }
132}
133
134impl FootnoteType for e::FootnoteReference {
135    fn is_auto(&self) -> bool {
136        self.extra().auto.is_auto()
137    }
138    fn is_symbol(&self) -> bool {
139        self.extra().auto.is_symbol()
140    }
141}
142
143impl Image {
144    #[must_use]
145    pub fn new(uri: Url) -> Image {
146        Image {
147            uri,
148            align: None,
149            alt: None,
150            height: None,
151            width: None,
152            scale: None,
153            target: None,
154        }
155    }
156}