Skip to content

Commit

Permalink
Strip markdown from files
Browse files Browse the repository at this point in the history
  • Loading branch information
arranf committed Mar 2, 2019
1 parent 752f9f6 commit d7ef7d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ matrix:
deploy:
provider: releases
api_key:
secure: VR3WQ8iRxCNKqFK/2HnNiph+JfHW3+9X3v5C89VVlvsY+Q6aT2VyLgiO+fVHJFamxq79m6khRIlfSroQ6HO29zpKivIR9wMqKUhcku1Er326iJPAo6fkwdOUym9Wg5SqWxGqEzkAVwrz8H+DX620vMOJSPeou7DZjCsWli+cCvFyftKezhamqPL4/v1NAjcHHZMTOb1CcKpSpv2oBS5uoZpvN21/fvvLkQeQhdcwD4kgaHkh6ch8FpG3q/ERtpxZc8UL+hXHuTHzzPyU8ph3qlJaOnaR3aCYE4cH5UadgxcaQH0vb0uKOo3PO0LUsBY0odcoU5CIBJ6IHqqZpXAbCDcNLoBx4FLcp+shH70UkrG6zrnEgw1zOr2kbsRZCfBrN7s/OW4VXFp5vFNjBmhKTD84uPKo9H7LEXbt6hsjLjIrtpyU+hKUwHXS0KkcuiHQeMw8YFYsTfayVLgU0v7tPdLlTAm1yFeJkp0PuuD+Mb1G26K1K78Ukduzzctr+jo5Yy5rjchPw3h5Ubmct4NUpsBwsMWiLyL0xFQYBIsSt1ju/4Z8MwjhgUJjsNH+bqdVmrgLQ5uY/xO7SMaYxqj9Gs5BV2EkkYoxg7RPfVYsF7ZSMaM4eQiN221TzdiiYO3H/MJSLfrVM1eXPRb4ZraKu+t5WE5+NHqJSdEuRGiW2XA=
file: "./target/release/hugo_to_json"
skip_cleanup: true
secure: D03D3NDfia4Dm8JFryiizkzpgmch5ptR+b4Kkap3KyTjd7A2tp3iO480Xi8ufULMM0F5FVSdXKVj/VGAf7LQPLJve+IxXIQzOagnbhLZ2VDKiyLiDK1vkZ6Rsz5cLDN8ncBIpfIg1c8AZ1vURzIfk+beZqSEys92k1AdxFYWG59U1TvmntfGGw2oICMNUBsf+SCx+Sf5qavr5Nl5qlZbKd70y1FowjvYzc1iqYWsUG7Ykh9HKUARAaVP3nJFLfEy7RGUiDZmexCFKJTG8NJ5zMvY9QA84Dgkz1Ig79mV2JjmwF6OE9lX6K/9GsJSyZRN7f67iPtujgYEYmdU2DfFnZ1q04VYYfe7JVZN3fKIOQl7jLm56ofWD+BuwJaM2io10OrFhb9M/rUyiA9N9t4UroCt8yVsZfOuw6i7nwGOsJHe4F0fOcZNZESU0zb8G6MdzGqB9QHS7VOdWOd2fdRDuPg+BF7jJESuh0ix2ejIYVaLD5IB0NQgf1IGmbK74NaS7Vma2jHVNPleqneU7zfOFnm2kuhaKJrrP4wLgwIsaTl/xNuHsnusopONpa6AvqUP7J2t6zzVJDCJm7Rdo/bGFmtp32717MtB1v32o/AS8MuAkLcjiF7JsCkgGOZqtMi+54HRDRycSHMVRwxxli2wTgofQSIeZ7/vqEVsl39nH0U=
file: "./target/release/hugo_to_json"
on:
tags: true
notifications:
email: false
email: false
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hugo_to_json"
version = "0.2.2"
version = "0.2.3"
authors = ["Arran France <arran@arranfrance.com>"]
edition = "2018"

Expand All @@ -9,11 +9,11 @@ toml = "0.4.10"
yaml-rust = "0.4"
walkdir = "2"
serde_json = "1.0"
serde = "1.0.84"
serde = "1.0.89"
serde_derive = "1.0"
log = "0.4.6"
env_logger = "0.6.0"

strip_markdown = "0.1"

[dev-dependencies]
assert_cmd = "0.11.0"
Expand Down
1 change: 0 additions & 1 deletion ToDo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
* Command line documentation
* Switch to command line parsing using StructOpt/Clap
* Add verbosity flags
* Strip markdown or convert to HTML
* Add flag for including drafts
* Allow directories to be excluded/included
33 changes: 18 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate log;
#[macro_use]
extern crate serde_derive;
extern crate yaml_rust;
extern crate strip_markdown;

pub mod hugo_to_json_error;
pub mod settings;
Expand All @@ -19,6 +20,7 @@ use std::path::{Path};
use toml::Value;
use walkdir::{DirEntry, WalkDir};
use yaml_rust::{YamlLoader};
use strip_markdown::strip_markdown;

use operation_result::*;
use hugo_to_json_error::*;
Expand Down Expand Up @@ -92,6 +94,7 @@ fn process_file(file_location: &FileLocation) -> Result<page_index::PageIndex, O
fn process_md_file(file_location: &FileLocation) -> Result<page_index::PageIndex, OperationResult> {
let contents = fs::read_to_string(file_location.absolute_path.to_string())?;

// Gets the first non-empty line of the file
let mut first_line = "";
let mut lines = contents.lines();

Expand All @@ -103,14 +106,14 @@ fn process_md_file(file_location: &FileLocation) -> Result<page_index::PageIndex
}

match first_line.chars().next() {
Some('+') => process_toml_front_matter(&contents, &file_location),
Some('-') => process_yaml_front_matter(&contents, &file_location),
Some('+') => process_md_toml_front_matter(&contents, &file_location),
Some('-') => process_md_yaml_front_matter(&contents, &file_location),
// TODO: JSON frontmatter '{' => process_json_frontmatter()
_ => Err(OperationResult::Parse(ParseError::new(&file_location.absolute_path, "Could not determine file front matter type.")))
}
}

fn process_toml_front_matter(contents: &str, file_location: &FileLocation) -> Result<page_index::PageIndex, OperationResult> {
fn process_md_toml_front_matter(contents: &str, file_location: &FileLocation) -> Result<page_index::PageIndex, OperationResult> {
let split_content: Vec<&str> = contents.trim().split(constants::TOML_FENCE).collect();

let length = split_content.len();
Expand Down Expand Up @@ -155,12 +158,12 @@ fn process_toml_front_matter(contents: &str, file_location: &FileLocation) -> Re
.filter_map(|v| v.as_str().map(|s| s.trim().to_owned()))
.collect();

let content = split_content[length - 1].trim().to_owned();
let content = strip_markdown(split_content[length - 1].trim());

page_index::PageIndex::new(title, slug, date, description, categories, series, tags, keywords, content, &file_location)
}

fn process_yaml_front_matter(contents: &str, file_location: &FileLocation) -> Result<page_index::PageIndex, OperationResult> {
fn process_md_yaml_front_matter(contents: &str, file_location: &FileLocation) -> Result<page_index::PageIndex, OperationResult> {
let split_content: Vec<&str> = contents.trim().split(constants::YAML_FENCE).collect();
let length = split_content.len();
if length <= 1 {
Expand Down Expand Up @@ -205,7 +208,7 @@ fn process_yaml_front_matter(contents: &str, file_location: &FileLocation) -> Re
.filter_map(|v| v.as_str().map(|s| s.trim().to_owned()))
.collect();

let content = split_content[length - 1].trim().to_owned();
let content = strip_markdown(split_content[length - 1].trim());

PageIndex::new(title, slug, date, description, categories, series, tags, keywords, content, &file_location)
}
Expand Down Expand Up @@ -242,7 +245,7 @@ tags:
---
The state of images on the web is pretty rough. What should be an easy goal, showing a user a picture, is...
"#);
let page_index = process_yaml_front_matter(&contents, &build_file_location());
let page_index = process_md_yaml_front_matter(&contents, &build_file_location());
assert!(page_index.is_ok());
let page_index = page_index.unwrap();
assert_eq!(page_index.title, "Responsive Blog Images");
Expand Down Expand Up @@ -273,7 +276,7 @@ tags:
---
The state of images on the web is pretty rough. What should be an easy goal, showing a user a picture, is...
"#);
let page_index = process_yaml_front_matter(&contents, &build_file_location());
let page_index = process_md_yaml_front_matter(&contents, &build_file_location());
assert!(page_index.is_err());
// Pattern match the error type
match page_index.unwrap_err() {
Expand All @@ -294,7 +297,7 @@ tags:
- Hugo
- Images
"#);
let page_index = process_yaml_front_matter(&contents, &build_file_location());
let page_index = process_md_yaml_front_matter(&contents, &build_file_location());
assert!(page_index.is_ok());
}

Expand All @@ -310,7 +313,7 @@ tags
- Images
---
"#);
let page_index = process_yaml_front_matter(&contents, &build_file_location());
let page_index = process_md_yaml_front_matter(&contents, &build_file_location());
assert!(page_index.is_err());
// Pattern match error
match page_index.unwrap_err() {
Expand All @@ -334,7 +337,7 @@ aliases = ['/evaluating-software-design/']
Design is iterative
"#);
let page_index = process_toml_front_matter(&contents, &build_file_location());
let page_index = process_md_toml_front_matter(&contents, &build_file_location());
assert!(page_index.is_ok());
let page_index = page_index.unwrap();
assert_eq!(page_index.title, "Evaluating Software Design");
Expand Down Expand Up @@ -362,7 +365,7 @@ tags = ['software development', 'revision', 'design']
Design is iterative
"#);
let page_index = process_toml_front_matter(&contents, &build_file_location());
let page_index = process_md_toml_front_matter(&contents, &build_file_location());
assert!(page_index.is_err());
// Pattern match error
match page_index.unwrap_err() {
Expand All @@ -383,7 +386,7 @@ tags = ['software development', 'revision', 'design']
Design is iterative
"#);
let page_index = process_toml_front_matter(&contents, &build_file_location());
let page_index = process_md_toml_front_matter(&contents, &build_file_location());
assert!(page_index.is_err());
// Pattern match error
match page_index.unwrap_err() {
Expand All @@ -404,7 +407,7 @@ tags = ['software development', 'revision', 'design']
Design is iterative
"#);
let page_index = process_toml_front_matter(&contents, &build_file_location());
let page_index = process_md_toml_front_matter(&contents, &build_file_location());
assert!(page_index.is_err());
// Pattern match error
match page_index.unwrap_err() {
Expand All @@ -426,7 +429,7 @@ tags = ['software development', 'revision', 'design']
Design is iterative
"#);
let page_index = process_toml_front_matter(&contents, &build_file_location());
let page_index = process_md_toml_front_matter(&contents, &build_file_location());
assert!(page_index.is_err());
// Pattern match error
match page_index.unwrap_err() {
Expand Down

0 comments on commit d7ef7d3

Please sign in to comment.