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
#[cfg(feature = "fifthtry")]
pub mod api;
#[cfg(feature = "fifthtry")]
pub mod code;
#[cfg(feature = "fifthtry")]
pub mod definition_list;
#[cfg(feature = "fifthtry")]
pub mod document;
#[cfg(feature = "fifthtry")]
pub mod heading;
#[cfg(feature = "fifthtry")]
pub mod iframe;
#[cfg(feature = "fifthtry")]
pub mod image;
#[cfg(feature = "fifthtry")]
pub mod include;
#[cfg(feature = "fifthtry")]
pub mod latex;
#[cfg(feature = "fifthtry")]
pub mod markdown;
#[cfg(feature = "fifthtry")]
pub mod meta;
pub mod p1;
#[cfg(feature = "fifthtry")]
pub mod p2;
#[cfg(feature = "fifthtry")]
pub mod pr;
#[cfg(feature = "fifthtry")]
pub mod prelude;
#[cfg(feature = "fifthtry")]
pub mod raw;
#[cfg(feature = "fifthtry")]
pub mod render;
#[cfg(feature = "fifthtry")]
pub mod rst;
#[cfg(feature = "fifthtry")]
pub mod section;
#[cfg(feature = "fifthtry")]
pub mod toc;
#[cfg(feature = "fifthtry")]
pub mod utils;
#[cfg(feature = "fifthtry")]
mod value_with_default;
#[cfg(feature = "fifthtry")]
pub mod youtube;

#[cfg(feature = "fifthtry")]
pub use crate::code::{Code, Highlight, ShowLineNumbers};
#[cfg(feature = "fifthtry")]
pub use crate::definition_list::DefinitionList;
#[cfg(feature = "fifthtry")]
pub use crate::document::{Align, Document, Table, TextDirection};
#[cfg(feature = "fifthtry")]
pub use crate::heading::Heading;
#[cfg(feature = "fifthtry")]
pub use crate::iframe::IFrame;
#[cfg(feature = "fifthtry")]
pub use crate::image::Image;
#[cfg(feature = "fifthtry")]
pub use crate::latex::Latex;
#[cfg(feature = "fifthtry")]
pub use crate::markdown::Markdown;
#[cfg(feature = "fifthtry")]
pub use crate::meta::{Admin, Meta, Reader, Someone, Surfer, Writer};
#[cfg(feature = "fifthtry")]
pub use crate::rst::Rst;
#[cfg(feature = "fifthtry")]
pub use crate::section::Section;
#[cfg(feature = "fifthtry")]
pub use crate::toc::{ToC, TocItem};
#[cfg(feature = "fifthtry")]
pub use crate::value_with_default::ValueWithDefault;
#[cfg(feature = "fifthtry")]
pub use crate::youtube::YouTube;

#[cfg(feature = "fifthtry")]
#[derive(serde_derive::Serialize, Eq, PartialEq, Debug, Default, Clone)]
pub struct Rendered {
    pub original: String,
    pub rendered: String,
}

#[cfg(not(feature = "fifthtry"))]
#[derive(Eq, PartialEq, Debug, Default, Clone)]
pub struct Rendered {
    pub original: String,
    pub rendered: String,
}

#[cfg(feature = "fifthtry")]
impl Rendered {
    pub fn rst(s: &str) -> Rendered {
        let p = match rst_parser::parse(s) {
            Ok(p) => p,
            Err(e) => return Rendered::line(format!("invalid rst: {}", e).as_str()),
        };

        let mut o: Vec<u8> = Vec::new();

        if let Err(e) = rst_renderer::render_html(&p, &mut o, true) {
            return Rendered::line(format!("invalid rst: {}", e).as_str());
        };

        Rendered {
            original: s.to_string(),
            rendered: match std::str::from_utf8(&o) {
                Ok(v) => v.to_string(),
                Err(e) => return Rendered::line(format!("invalid rst: {}", e).as_str()),
            },
        }
    }

    pub fn from(s: &str) -> Rendered {
        Rendered {
            original: s.to_string(),
            rendered: render::render(s, true, false),
        }
    }

    pub fn from_extra(s: &str, auto_links: bool, hard_breaks: bool) -> Rendered {
        Rendered {
            original: s.to_string(),
            rendered: render::render(s, auto_links, hard_breaks),
        }
    }

    pub fn latex(s: &str) -> Result<Rendered, crate::document::ParseError> {
        let opts = katex::Opts::builder()
            .throw_on_error(false)
            .display_mode(true)
            .build()
            .unwrap();

        Ok(Rendered {
            original: s.to_string(),
            rendered: katex::render_with_opts(s, &opts).map_err(|e| match e {
                katex::Error::JsValueError(s) | katex::Error::JsExecError(s) => {
                    crate::document::ParseError::ValidationError(s)
                }
                katex::Error::JsInitError(s) => {
                    panic!("{}", s)
                }
                _ => todo!(),
            })?,
        })
    }

    pub fn code(code: &str, ext: &str) -> Rendered {
        Rendered {
            original: code.to_string(),
            rendered: render::code(code.replace("\n\\-- ", "\n-- ").as_str(), ext),
        }
    }

    pub fn line(s: &str) -> Rendered {
        Rendered {
            original: s.to_string(),
            rendered: render::inline(s),
        }
    }
}