1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use libxml::parser::XmlParseError;
use libxml::readonly::RoNode;
use libxml::tree::Document as XmlDoc;
use libxml::xpath::Context;

use super::corpus::Corpus;
use super::{DNMRangeIterator, RoNodeIterator};
use crate::dnm::DNM;

/// One of our math documents, thread-friendly
pub struct Document<'d> {
  /// The DOM of the document
  pub dom: XmlDoc,
  /// The file path of the document
  pub path: String,
  /// A reference to the corpus containing this document
  pub corpus: &'d Corpus,
  /// If it exists, the DNM corresponding to this document
  pub dnm: Option<DNM>,
}

impl<'d> Document<'d> {
  /// Load a new document
  pub fn new(filepath: String, corpus: &'d Corpus) -> Result<Self, XmlParseError> {
    let dom = if filepath.ends_with(".xhtml") {
      corpus.xml_parser.parse_file(&filepath)?
    } else {
      corpus.html_parser.parse_file(&filepath)?
    };

    Ok(Document {
      path: filepath,
      dom,
      corpus,
      dnm: None,
    })
  }

  /// Obtain the problem-free logical headings of a libxml `Document`
  pub fn get_heading_nodes(&self) -> Vec<RoNode> { Document::heading_nodes(&self.dom) }
  /// Associated function for `get_heading_nodes`
  fn heading_nodes(doc: &XmlDoc) -> Vec<RoNode> {
    Document::xpath_nodes(doc,
      "//*[contains(@class,'ltx_title') and not(contains(@class,'ltx_title_document')) and (local-name()='h1' or local-name()='h2' or local-name()='h3' or local-name()='h4' or local-name()='h5' or local-name()='h6') and not(descendant::*[contains(@class,'ltx_ERROR')])]",
    )
  }
  /// Get an iterator over the headings of the document
  pub fn heading_iter(&self) -> RoNodeIterator {
    RoNodeIterator {
      walker: Document::heading_nodes(&self.dom).into_iter(),
      document: self,
    }
  }

  /// Obtain the problem-free logical paragraphs of a libxml `Document`
  pub fn get_paragraph_nodes(&self) -> Vec<RoNode> { Document::paragraph_nodes(&self.dom) }

  /// Associated function for `get_paragraph_nodes`
  fn paragraph_nodes(doc: &XmlDoc) -> Vec<RoNode> {
    Document::xpath_nodes(doc,
      "//*[local-name()='div' and contains(@class,'ltx_para') and not(descendant::*[contains(@class,'ltx_ERROR')]) and not(preceding-sibling::*[contains(@class,'ltx_ERROR')])]",
    )
  }

  /// Get an iterator over the paragraphs of the document
  pub fn paragraph_iter(&self) -> RoNodeIterator {
    RoNodeIterator {
      walker: Document::paragraph_nodes(&self.dom).into_iter(),
      document: self,
    }
  }

  /// Obtain the first paragraph of a marked up article abstract (<div class="ltx_abstract"><p>)
  fn abstract_p_node(doc: &XmlDoc) -> Option<RoNode> {
    Document::xpath_node(doc,
      "//*[local-name()='div' and contains(@class,'ltx_abstract') and not(descendant::*[contains(@class,'ltx_ERROR')])]//*[local-name()='p' and contains(@class,'ltx_p')][1]")
  }

  /// Obtain the first <div class='ltx_keywords'>text content</div>
  /// which remains undetected by the regular paragraph selectors
  /// as deposited by LaTeXML for the \keywords macro
  fn keywords_node(doc: &XmlDoc) -> Option<RoNode> {
    Document::xpath_node(doc,
      "//*[local-name()='div' and contains(@class,'ltx_keywords') and not(descendant::*[contains(@class,'ltx_ERROR')])]")
  }

  /// Obtain the first <div class='ltx_acknowledgement'>text content</div>
  /// which remains undetected by the regular paragraph selectors
  /// as deposited by LaTeXML for the \acknowledgements macro
  fn acknowledgement_node(doc: &XmlDoc) -> Option<RoNode> {
    Document::xpath_node(doc,
      "//*[local-name()='div' and contains(@class,'ltx_acknowledgement') and not(descendant::*[contains(@class,'ltx_ERROR')])]")
  }

  /// Obtains all error-free marked captions (e.g. Figure and Table captions)
  fn caption_nodes(doc: &XmlDoc) -> Vec<RoNode> {
    Document::xpath_nodes(doc,
    "//*[local-name()='figcaption' and contains(@class,'ltx_caption') and not(descendant::*[contains(@class,'ltx_ERROR')])]")
  }

  /// Get an iterator over textual paragraphs of the document, in a loose sense,
  /// contents: abstract (first p), keywords, logical paragraphs, acknowledgement, table/figure
  /// captions
  pub fn extended_paragraph_iter(&self) -> RoNodeIterator {
    let mut paras = Vec::new();
    if let Some(anode) = Document::abstract_p_node(&self.dom) {
      paras.push(anode);
    }
    if let Some(keywords) = Document::keywords_node(&self.dom) {
      paras.push(keywords);
    }
    paras.extend(Document::paragraph_nodes(&self.dom));
    if let Some(anode) = Document::acknowledgement_node(&self.dom) {
      paras.push(anode);
    }
    paras.extend(Document::caption_nodes(&self.dom));

    RoNodeIterator {
      walker: paras.into_iter(),
      document: self,
    }
  }

  /// Obtain the MathML <math> nodes of a libxml `Document`
  pub fn get_math_nodes(&self) -> Vec<RoNode> { Document::math_nodes(&self.dom) }

  /// Associated function for `get_math_nodes`
  pub(crate) fn math_nodes(doc: &XmlDoc) -> Vec<RoNode> {
    Document::xpath_nodes(doc, "//*[local-name()='math']")
  }
  /// Obtain the <span[class=ltx_ref]> nodes of a libxml `Document`
  pub fn get_ref_nodes(&self) -> Vec<RoNode> { Document::ref_nodes(&self.dom) }
  /// Associated function for `get_ref_nodes`
  pub(crate) fn ref_nodes(doc: &XmlDoc) -> Vec<RoNode> {
    Document::xpath_nodes(doc,
    "//*[(local-name()='span' or local-name()='a') and (contains(@class,'ltx_ref ') or @class='ltx_ref')]")
  }

  /// Get an iterator over the sentences of the document
  pub fn sentence_iter(&mut self) -> DNMRangeIterator {
    if self.dnm.is_none() {
      if let Some(root) = self.dom.get_root_readonly() {
        self.dnm = Some(DNM::new(root, self.corpus.dnm_parameters.clone()));
      }
    }
    let tokenizer = &self.corpus.tokenizer;
    let sentences = tokenizer.sentences(self.dnm.as_ref().unwrap());
    DNMRangeIterator {
      walker: sentences.into_iter(),
      document: self,
    }
  }

  /// Obtain the nodes associated with the xpath evaluation over the underlying libxml `Document`
  pub fn get_xpath_nodes(&self, xpath_str: &str) -> Vec<RoNode> {
    Document::xpath_nodes(&self.dom, xpath_str)
  }

  /// Associated function for `get_xpath_nodes`
  pub(crate) fn xpath_nodes(doc: &XmlDoc, xpath_str: &str) -> Vec<RoNode> {
    let xpath_context = Context::new(doc).unwrap();
    match xpath_context.evaluate(xpath_str) {
      Ok(found_payload) => found_payload.get_readonly_nodes_as_vec(),
      _ => Vec::new(),
    }
  }

  /// Obtain the first node associated with the xpath evaluation over the underlying libxml
  /// `Document`
  pub fn get_xpath_node(&self, xpath_str: &str) -> Option<RoNode> {
    Document::xpath_node(&self.dom, xpath_str)
  }

  /// Associated function for `get_xpath_nodes`
  pub(crate) fn xpath_node(doc: &XmlDoc, xpath_str: &str) -> Option<RoNode> {
    let xpath_context = Context::new(doc).unwrap();
    match xpath_context.evaluate(xpath_str) {
      Ok(found_payload) => {
        let mut vec_nodes = found_payload.get_readonly_nodes_as_vec();
        if !vec_nodes.is_empty() {
          Some(vec_nodes.remove(0))
        } else {
          None
        }
      },
      _ => None,
    }
  }

  /// Get an iterator over a custom xpath selector over the document
  pub fn xpath_selector_iter(&self, xpath_str: &str) -> RoNodeIterator {
    RoNodeIterator {
      walker: Document::xpath_nodes(&self.dom, xpath_str).into_iter(),
      document: self,
    }
  }

  /// Associated function for `get_filtered_nodes`
  pub(crate) fn dfs_filter_nodes(node: RoNode, filter: &dyn Fn(&RoNode) -> bool) -> Vec<RoNode> {
    let mut found = Vec::new();
    if filter(&node) {
      found.push(node);
    }
    for child in node.get_child_nodes().into_iter() {
      found.extend(Document::dfs_filter_nodes(child, filter));
    }
    found
  }

  /// Get an iterator using a custom closure predicate filter over the document (depth-first
  /// descent)
  pub fn filter_iter(&self, filter: &dyn Fn(&RoNode) -> bool) -> RoNodeIterator {
    // TODO: Can this be lazy? Eager for now...
    RoNodeIterator {
      walker: Document::dfs_filter_nodes(self.dom.get_root_readonly().unwrap(), filter).into_iter(),
      document: self,
    }
  }
}