asciidoc_parser/content/content.rs
1//! Describes the content of a non-compound block after any relevant
2//! [substitutions] have been performed.
3//!
4//! [substitutions]: https://docs.asciidoctor.org/asciidoc/latest/subs/
5
6use crate::{Span, strings::CowStr};
7
8/// Describes the annotated content of a block after any relevant
9/// [substitutions] have been performed.
10///
11/// This is typically used to represent the main body of block types that don't
12/// contain other blocks, such as [`SimpleBlock`] or [`RawDelimitedBlock`].
13///
14/// [substitutions]: https://docs.asciidoctor.org/asciidoc/latest/subs/
15/// [`SimpleBlock`]: crate::blocks::SimpleBlock
16/// [`RawDelimitedBlock`]: crate::blocks::RawDelimitedBlock
17#[derive(Clone, Debug, Eq, PartialEq)]
18pub struct Content<'src> {
19 /// The original [`Span`] from which this content was derived.
20 original: Span<'src>,
21
22 /// The possibly-modified text after substititions have been performed.
23 pub(crate) rendered: CowStr<'src>,
24}
25
26impl<'src> Content<'src> {
27 /// Returns the original span from which this [`Content`] was derived.
28 ///
29 /// This is the source text before any substitions have been applied.
30 pub fn original(&self) -> Span<'src> {
31 self.original
32 }
33
34 /// Returns the final text after all substitutions have been applied.
35 pub fn rendered(&'src self) -> &'src str {
36 self.rendered.as_ref()
37 }
38
39 /// Returns `true` if `self` contains no text.
40 pub fn is_empty(&self) -> bool {
41 self.rendered.as_ref().is_empty()
42 }
43
44 #[allow(unused)]
45 /// Replaces an exact string with another exact string.
46 pub(crate) fn replace_str(&mut self, from: &str, to: &'static str) {
47 if self.rendered.contains(from) {
48 self.rendered = self.rendered.replace(from, to).into();
49 }
50 }
51}
52
53impl<'src> From<Span<'src>> for Content<'src> {
54 fn from(span: Span<'src>) -> Self {
55 Self {
56 original: span,
57 rendered: CowStr::from(span.data()),
58 }
59 }
60}