Fixes bug with extra syntaxes in non-root section, adds tests.

This commit is contained in:
Nathan Ringo 2018-08-14 02:12:04 -05:00
parent 64fa890aa9
commit 9df5da4697
No known key found for this signature in database
GPG key ID: 3208D2FE5E229746
10 changed files with 83 additions and 18 deletions

View file

@ -166,12 +166,13 @@ impl Page {
/// We need access to all pages url to render links relative to content /// We need access to all pages url to render links relative to content
/// so that can't happen at the same time as parsing /// so that can't happen at the same time as parsing
pub fn render_markdown(&mut self, permalinks: &HashMap<String, String>, tera: &Tera, config: &Config, anchor_insert: InsertAnchor) -> Result<()> { pub fn render_markdown(&mut self, permalinks: &HashMap<String, String>, tera: &Tera, config: &Config, base_path: &Path, anchor_insert: InsertAnchor) -> Result<()> {
let mut context = RenderContext::new( let mut context = RenderContext::new(
tera, tera,
config, config,
&self.permalink, &self.permalink,
permalinks, permalinks,
base_path,
anchor_insert, anchor_insert,
); );

View file

@ -123,12 +123,13 @@ impl Section {
/// We need access to all pages url to render links relative to content /// We need access to all pages url to render links relative to content
/// so that can't happen at the same time as parsing /// so that can't happen at the same time as parsing
pub fn render_markdown(&mut self, permalinks: &HashMap<String, String>, tera: &Tera, config: &Config) -> Result<()> { pub fn render_markdown(&mut self, permalinks: &HashMap<String, String>, tera: &Tera, config: &Config, base_path: &Path) -> Result<()> {
let mut context = RenderContext::new( let mut context = RenderContext::new(
tera, tera,
config, config,
&self.permalink, &self.permalink,
permalinks, permalinks,
base_path,
self.meta.insert_anchor_links, self.meta.insert_anchor_links,
); );

View file

@ -3,6 +3,7 @@ extern crate lazy_static;
extern crate syntect; extern crate syntect;
use std::cell::RefCell; use std::cell::RefCell;
use std::path::Path;
use syntect::LoadingError; use syntect::LoadingError;
use syntect::dumps::from_binary; use syntect::dumps::from_binary;
@ -23,12 +24,12 @@ lazy_static! {
pub static ref THEME_SET: ThemeSet = from_binary(include_bytes!("../../../sublime_themes/all.themedump")); pub static ref THEME_SET: ThemeSet = from_binary(include_bytes!("../../../sublime_themes/all.themedump"));
} }
pub fn get_highlighter<'a>(theme: &'a Theme, info: &str, extra_syntaxes: &[String]) -> Result<HighlightLines<'a>, LoadingError> { pub fn get_highlighter<'a>(theme: &'a Theme, info: &str, base_path: &Path, extra_syntaxes: &[String]) -> Result<HighlightLines<'a>, LoadingError> {
SYNTAX_SET.with(|rc| { SYNTAX_SET.with(|rc| {
let (ss, extras_added) = &mut *rc.borrow_mut(); let (ss, extras_added) = &mut *rc.borrow_mut();
if !*extras_added { if !*extras_added {
for dir in extra_syntaxes { for dir in extra_syntaxes {
ss.load_syntaxes(dir, true)?; ss.load_syntaxes(base_path.join(dir), true)?;
} }
ss.link_syntaxes(); ss.link_syntaxes();
*extras_added = true; *extras_added = true;

View file

@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::path::Path;
use tera::{Tera, Context}; use tera::{Tera, Context};
use front_matter::InsertAnchor; use front_matter::InsertAnchor;
@ -13,6 +14,7 @@ pub struct RenderContext<'a> {
pub tera_context: Context, pub tera_context: Context,
pub current_page_permalink: &'a str, pub current_page_permalink: &'a str,
pub permalinks: &'a HashMap<String, String>, pub permalinks: &'a HashMap<String, String>,
pub base_path: &'a Path,
pub insert_anchor: InsertAnchor, pub insert_anchor: InsertAnchor,
} }
@ -22,6 +24,7 @@ impl<'a> RenderContext<'a> {
config: &'a Config, config: &'a Config,
current_page_permalink: &'a str, current_page_permalink: &'a str,
permalinks: &'a HashMap<String, String>, permalinks: &'a HashMap<String, String>,
base_path: &'a Path,
insert_anchor: InsertAnchor, insert_anchor: InsertAnchor,
) -> RenderContext<'a> { ) -> RenderContext<'a> {
let mut tera_context = Context::new(); let mut tera_context = Context::new();
@ -32,6 +35,7 @@ impl<'a> RenderContext<'a> {
current_page_permalink, current_page_permalink,
permalinks, permalinks,
insert_anchor, insert_anchor,
base_path,
config, config,
} }
} }

View file

@ -97,7 +97,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<(Strin
} }
let theme = &THEME_SET.themes[&context.config.highlight_theme]; let theme = &THEME_SET.themes[&context.config.highlight_theme];
match get_highlighter(&theme, info, &context.config.extra_syntaxes) { match get_highlighter(&theme, info, context.base_path, &context.config.extra_syntaxes) {
Ok(h) => highlighter = Some(h), Ok(h) => highlighter = Some(h),
Err(err) => { Err(err) => {
error = Some(format!("Could not load syntax: {}", err).into()); error = Some(format!("Could not load syntax: {}", err).into());

View file

@ -258,6 +258,7 @@ impl Site {
let permalinks = &self.permalinks; let permalinks = &self.permalinks;
let tera = &self.tera; let tera = &self.tera;
let config = &self.config; let config = &self.config;
let base_path = &self.base_path;
// TODO: avoid the duplication with function above for that part // TODO: avoid the duplication with function above for that part
// This is needed in the first place because of silly borrow checker // This is needed in the first place because of silly borrow checker
@ -269,13 +270,13 @@ impl Site {
self.pages.par_iter_mut() self.pages.par_iter_mut()
.map(|(_, page)| { .map(|(_, page)| {
let insert_anchor = pages_insert_anchors[&page.file.path]; let insert_anchor = pages_insert_anchors[&page.file.path];
page.render_markdown(permalinks, tera, config, insert_anchor) page.render_markdown(permalinks, tera, config, base_path, insert_anchor)
}) })
.fold(|| Ok(()), Result::and) .fold(|| Ok(()), Result::and)
.reduce(|| Ok(()), Result::and)?; .reduce(|| Ok(()), Result::and)?;
self.sections.par_iter_mut() self.sections.par_iter_mut()
.map(|(_, section)| section.render_markdown(permalinks, tera, config)) .map(|(_, section)| section.render_markdown(permalinks, tera, config, base_path))
.fold(|| Ok(()), Result::and) .fold(|| Ok(()), Result::and)
.reduce(|| Ok(()), Result::and)?; .reduce(|| Ok(()), Result::and)?;
@ -318,7 +319,7 @@ impl Site {
if render { if render {
let insert_anchor = self.find_parent_section_insert_anchor(&self.pages[&path].file.parent); let insert_anchor = self.find_parent_section_insert_anchor(&self.pages[&path].file.parent);
let page = self.pages.get_mut(&path).unwrap(); let page = self.pages.get_mut(&path).unwrap();
page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?; page.render_markdown(&self.permalinks, &self.tera, &self.config, &self.base_path, insert_anchor)?;
} }
Ok(prev) Ok(prev)
@ -335,7 +336,7 @@ impl Site {
if render { if render {
let section = self.sections.get_mut(&path).unwrap(); let section = self.sections.get_mut(&path).unwrap();
section.render_markdown(&self.permalinks, &self.tera, &self.config)?; section.render_markdown(&self.permalinks, &self.tera, &self.config, &self.base_path)?;
} }
Ok(prev) Ok(prev)

View file

@ -19,7 +19,7 @@ fn can_parse_site() {
site.load().unwrap(); site.load().unwrap();
// Correct number of pages (sections are pages too) // Correct number of pages (sections are pages too)
assert_eq!(site.pages.len(), 14); assert_eq!(site.pages.len(), 15);
let posts_path = path.join("content").join("posts"); let posts_path = path.join("content").join("posts");
// Make sure we remove all the pwd + content from the sections // Make sure we remove all the pwd + content from the sections
@ -44,7 +44,7 @@ fn can_parse_site() {
let posts_section = &site.sections[&posts_path.join("_index.md")]; let posts_section = &site.sections[&posts_path.join("_index.md")];
assert_eq!(posts_section.subsections.len(), 1); assert_eq!(posts_section.subsections.len(), 1);
assert_eq!(posts_section.pages.len(), 6); assert_eq!(posts_section.pages.len(), 7);
let tutorials_section = &site.sections[&posts_path.join("tutorials").join("_index.md")]; let tutorials_section = &site.sections[&posts_path.join("tutorials").join("_index.md")];
assert_eq!(tutorials_section.subsections.len(), 2); assert_eq!(tutorials_section.subsections.len(), 2);
@ -321,22 +321,41 @@ fn can_build_site_with_pagination_for_section() {
"posts/page/1/index.html", "posts/page/1/index.html",
"http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\"" "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
)); ));
assert!(file_contains!(public, "posts/index.html", "Num pagers: 3")); assert!(file_contains!(public, "posts/index.html", "Num pagers: 4"));
assert!(file_contains!(public, "posts/index.html", "Page size: 2")); assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/index.html", "Current index: 1")); assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
assert!(!file_contains!(public, "posts/index.html", "has_prev"));
assert!(file_contains!(public, "posts/index.html", "has_next")); assert!(file_contains!(public, "posts/index.html", "has_next"));
assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/")); assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/")); assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false); assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
assert!(file_exists!(public, "posts/page/2/index.html")); assert!(file_exists!(public, "posts/page/2/index.html"));
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 3")); assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 4"));
assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2")); assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2")); assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
assert!(file_contains!(public, "posts/page/2/index.html", "has_prev")); assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
assert!(file_contains!(public, "posts/page/2/index.html", "has_next")); assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/")); assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/")); assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
assert!(file_exists!(public, "posts/page/3/index.html"));
assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 4"));
assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
assert!(file_contains!(public, "posts/page/3/index.html", "has_next"));
assert!(file_contains!(public, "posts/page/3/index.html", "First: https://replace-this-with-your-url.com/posts/"));
assert!(file_contains!(public, "posts/page/3/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
assert!(file_exists!(public, "posts/page/4/index.html"));
assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 4"));
assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
assert!(!file_contains!(public, "posts/page/4/index.html", "has_next"));
assert!(file_contains!(public, "posts/page/4/index.html", "First: https://replace-this-with-your-url.com/posts/"));
assert!(file_contains!(public, "posts/page/4/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
} }
#[test] #[test]
@ -397,10 +416,10 @@ fn can_build_rss_feed() {
assert!(Path::new(&public).exists()); assert!(Path::new(&public).exists());
assert!(file_exists!(public, "rss.xml")); assert!(file_exists!(public, "rss.xml"));
// latest article is posts/simple.md // latest article is posts/extra-syntax.md
assert!(file_contains!(public, "rss.xml", "Extra Syntax"));
// Next is posts/simple.md
assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes")); assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
// Next is posts/python.md
assert!(file_contains!(public, "rss.xml", "Python in posts"));
} }
@ -420,3 +439,20 @@ fn can_build_search_index() {
assert!(file_exists!(public, "elasticlunr.min.js")); assert!(file_exists!(public, "elasticlunr.min.js"));
assert!(file_exists!(public, "search_index.en.js")); assert!(file_exists!(public, "search_index.en.js"));
} }
#[test]
fn can_build_with_extra_syntaxes() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
site.load().unwrap();
let tmp_dir = tempdir().expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
assert!(&public.exists());
assert!(file_exists!(public, "posts/extra-syntax/index.html"));
assert!(file_contains!(public, "posts/extra-syntax/index.html",
r#"<span style="background-color:#2b303b;color:#d08770;">test</span>"#));
}

View file

@ -10,5 +10,7 @@ taxonomies = [
{name = "categories", rss = true}, {name = "categories", rss = true},
] ]
extra_syntaxes = ["syntaxes"]
[extra.author] [extra.author]
name = "Vincent Prouillet" name = "Vincent Prouillet"

View file

@ -0,0 +1,9 @@
+++
title = "Extra Syntax"
description = ""
date = 2018-08-14
+++
```test-syntax
This is a test code snippet.
```

View file

@ -0,0 +1,10 @@
%YAML 1.2
---
file_extensions:
- test-syntax
scope: source.test
contexts:
main:
- match: "test"
scope: constant.language.test