document_tree/
extra_attributes.rs

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