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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * libssg
 *
 * Copyright 2020 Manos Pitsidianakis
 *
 * This file is part of libssg.
 *
 * libssg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * libssg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libssg. If not, see <http://www.gnu.org/licenses/>.
 */

use super::*;

///`Compiler`s are functions or closures that transform resource files (think stylesheets, text in markdown, etc) to
/// something. A compiler that uses `pandoc` is provided, it expects a pandoc markdown file with
/// optional metadata in the preamble like so:
///
/// ```text
///  ---
/// title: example title
/// author: epilys
/// date: June 15, 2019
/// ---
///
/// Lorem ipsum.
/// ```
///
/// `Compiler`s' only obligation is transforming the contents of the given file `path` into a
/// `String` by adding it to the metadata map with the key `body`.
pub type Compiler = Box<dyn Fn(&mut State, &Path) -> Result<Value>>;

pub use pandoc::pandoc;
mod pandoc {
    use super::*;
    use serde::{self, Deserialize};
    use serde_json;
    use serde_json::{Map, Value};
    use std::collections::HashMap;
    pub fn pandoc() -> Compiler {
        Box::new(|state: &mut State, path: &Path| {
            let metadata = Command::new("pandoc")
                .args(&["-t", "json"])
                .arg(&path)
                .output()
                .map_err(|err| format!("failed to execute pandoc: {}", err))?;
            let pandoc_json: PandocJsonOutput =
                serde_json::from_str(&String::from_utf8_lossy(&metadata.stdout))
                    .unwrap_or_default();
            let mut metadata_map: Map<String, Value> = parse_metadata(pandoc_json);
            if state.verbosity() > 2 {
                println!(
                    "Parsed metadata for {}: {:#?}",
                    path.display(),
                    &metadata_map
                );
            }
            let output = Command::new("pandoc")
                .arg(&path)
                .output()
                .map_err(|err| format!("failed to execute pandoc: {}", err))?;
            metadata_map.insert(
                "body".to_string(),
                Value::String(String::from_utf8_lossy(&output.stdout).to_string()),
            );
            Ok(Value::Object(metadata_map))
        })
    }

    fn parse_metadata(output: PandocJsonOutput) -> Map<String, Value> {
        let meta = output.meta;

        meta.into_iter()
            .map(|(key, metaval)| (key, metaval.into()))
            .collect::<_>()
    }

    #[derive(Deserialize, Debug, Default)]
    struct PandocJsonOutput {
        blocks: Value,
        #[serde(rename = "pandoc-api-version")]
        pandoc_api_version: Value,
        meta: HashMap<String, PandocMetaValue>,
    }

    #[derive(Deserialize, Debug)]
    #[serde(tag = "t", content = "c")]
    enum PandocMetaValue {
        MetaMap(HashMap<String, PandocMetaValue>),
        MetaList(Vec<PandocMetaValue>),
        MetaBool(bool),
        MetaString(String),
        MetaInlines(Vec<PandocMetaInline>),
        MetaBlocks(Value),
    }

    impl Into<Value> for PandocMetaValue {
        fn into(self) -> Value {
            use PandocMetaValue::*;
            match self {
                MetaMap(map) => Value::Object(
                    map.into_iter()
                        .map(|(key, metaval)| (key.clone(), metaval.into()))
                        .collect(),
                ),
                MetaList(list) => Value::Array(
                    list.into_iter()
                        .map(|metaval| metaval.into())
                        .collect::<Vec<Value>>(),
                ),
                MetaBool(v) => Value::Bool(v),
                MetaString(v) => Value::String(v),
                MetaInlines(inlines_list) => Value::String(inlines_list.into_iter().fold(
                    String::new(),
                    |mut acc, inline| {
                        let inline: String = inline.into();
                        acc.extend(inline.chars());
                        acc
                    },
                )),
                MetaBlocks(_) => Value::String(String::new()),
            }
        }
    }

    #[derive(Deserialize, Debug)]
    #[serde(tag = "t", content = "c")]
    enum PandocMetaInline {
        Str(String),
        Emph(Vec<PandocMetaInline>),
        Strong(Vec<PandocMetaInline>),
        Strikeout(Vec<PandocMetaInline>),
        Superscript(Vec<PandocMetaInline>),
        Subscript(Vec<PandocMetaInline>),
        SmallCaps(Vec<PandocMetaInline>),
        Quoted(Value),
        Cite(Value),
        Code(Value),
        Space,
        SoftBreak,
        LineBreak,
        Math(Value),
        RawPandocMetaInline(Value),
        Link(Value),
        Image(Value),
        Note(Value),
        Span(Value),
    }

    impl Into<String> for PandocMetaInline {
        fn into(self) -> String {
            use PandocMetaInline::*;
            match self {
                Str(inner) => inner,
                Emph(list) => list.into_iter().fold(String::new(), |mut acc, el| {
                    let el: String = el.into();
                    acc.extend(el.chars());
                    acc
                }),
                Strong(list) => list.into_iter().fold(String::new(), |mut acc, el| {
                    let el: String = el.into();
                    acc.extend(el.chars());
                    acc
                }),
                Space => String::from(" "),
                SoftBreak => String::from("\n"),
                LineBreak => String::from("\n"),
                Strikeout(list) => list.into_iter().fold(String::new(), |mut acc, el| {
                    let el: String = el.into();
                    acc.extend(el.chars());
                    acc
                }),
                Superscript(list) => list.into_iter().fold(String::new(), |mut acc, el| {
                    let el: String = el.into();
                    acc.extend(el.chars());
                    acc
                }),
                Subscript(list) => list.into_iter().fold(String::new(), |mut acc, el| {
                    let el: String = el.into();
                    acc.extend(el.chars());
                    acc
                }),
                SmallCaps(list) => list.into_iter().fold(String::new(), |mut acc, el| {
                    let el: String = el.into();
                    acc.extend(el.chars());
                    acc
                }),
                Quoted(_) => String::new(),
                Cite(_) => String::new(),
                Code(_) => String::new(),
                Math(_) => String::new(),
                RawPandocMetaInline(_) => String::new(),
                Link(_) => String::new(),
                Image(_) => String::new(),
                Note(_) => String::new(),
                Span(_) => String::new(),
            }
        }
    }
}

pub use rss::*;

mod rss {
    use super::*;
    use serde::{self, Serialize};
    use serde_json::json;

    #[derive(Serialize)]
    pub struct RssItem {
        pub title: String,
        pub description: String,
        pub link: String,
        pub last_build_date: String,
        pub pub_date: String,
        pub ttl: i32,
    }

    const RSS_TEMPLATE: &'static str = r#"<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>{{ config.title }}</title>
  <description><![CDATA[{{ include config.description }}]]></description>
  <link>{{ config.link }}</link>
  <atom:link href="{{ config.link }}/{{ path }}" rel="self" type="application/rss+xml" />
  <pubDate> Thu, 01 Jan 1970 00:00:00 +0000 </pubDate>
 {{#each items}}
 <item>
  <title>{{ title }}</title>
  <description><![CDATA[{{ include description }}]]></description>
  <link>{{ config.link }}{{ link }}</link>
  <guid>{{ config.link }}{{ link }}</guid>
  <pubDate>{{ pub_date }}</pubDate>
 </item>
{{/each~}}

</channel>
</rss>"#;
    pub fn rss_feed(snapshot_name: String, configuration: RssItem) -> Compiler {
        Box::new(move |state: &mut State, dest_path: &Path| {
            if !state.snapshots.contains_key(&snapshot_name) {
                // No posts configured/found
                Err(format!("There are no snapshots with key `{}`, is the source rule empty (ie producing no items) or have you typed the name wrong?", &snapshot_name))?;
            }

            let snapshot = &state.snapshots[&snapshot_name];
            let mut rss_items = Vec::with_capacity(snapshot.len());
            for artifact in snapshot.iter() {
                if let Value::Object(ref map) = &state.artifacts[&artifact].metadata {
                    macro_rules! get_property {
                        ($key:literal, $default:expr) => {
                            map.get($key)
                                .and_then(|t| {
                                    if let Value::String(ref var) = t {
                                        Some(var.to_string())
                                    } else {
                                        None
                                    }
                                })
                                .unwrap_or_else(|| $default)
                        };
                    }
                    rss_items.push(RssItem {
                        title: get_property!("title", format!("No title, uuid: {}", artifact)),
                        description: get_property!("body", String::new()),
                        link: format!(
                            "{}/{}",
                            &configuration.link,
                            &state.artifacts[&artifact].path.display()
                        ),
                        last_build_date: String::new(),
                        pub_date: get_property!(
                            "date",
                            "Thu, 01 Jan 1970 00:00:00 +0000".to_string()
                        ),
                        ttl: 1800,
                    });
                }
            }
            let mut handlebars = Handlebars::new();
            handlebars.register_helper("include", Box::new(include_helper));

            let test = handlebars.render_template(
                RSS_TEMPLATE,
                &json!({ "items": rss_items, "config": configuration, "path": dest_path }),
            )?;
            Ok(json!({ "body": test }))
        })
    }
}

pub fn compiler_seq(compiler_a: Compiler, compiler_b: Compiler) -> Compiler {
    Box::new(move |state: &mut State, path: &Path| {
        let a = compiler_a(state, &path)?;
        let b = compiler_b(state, &path)?;
        let a = match (a, b) {
            (Value::Object(mut map_a), Value::Object(map_b)) => {
                map_a.extend(map_b.into_iter());
                Value::Object(map_a)
            }
            (a, _) => a,
        };
        Ok(a)
    })
}