Fix formatting
This commit is contained in:
parent
dacd1d16bc
commit
6f832ed434
|
@ -24,7 +24,8 @@ use topological_sort::TopologicalSort;
|
|||
|
||||
use crate::errors::*;
|
||||
use crate::preprocess::{
|
||||
CmdPreprocessor, DrinkPreprocessor, IndexPreprocessor, LinkPreprocessor, Preprocessor, PreprocessorContext,
|
||||
CmdPreprocessor, DrinkPreprocessor, IndexPreprocessor, LinkPreprocessor, Preprocessor,
|
||||
PreprocessorContext,
|
||||
};
|
||||
use crate::renderer::{CmdRenderer, HtmlHandlebars, MarkdownRenderer, RenderContext, Renderer};
|
||||
use crate::utils;
|
||||
|
|
|
@ -2,15 +2,15 @@ use crate::errors::*;
|
|||
|
||||
use super::{Preprocessor, PreprocessorContext};
|
||||
use crate::book::{Book, BookItem, Chapter};
|
||||
use regex::{Captures, Regex};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::fs::File;
|
||||
use regex::{Captures, Regex};
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
const SPLITTER: char = ':';
|
||||
|
||||
type Dict=HashMap<String, String>;
|
||||
type Dict = HashMap<String, String>;
|
||||
|
||||
/// DRY Links - A preprocessor for using centralized links collection:
|
||||
///
|
||||
|
@ -29,18 +29,17 @@ impl DrinkPreprocessor {
|
|||
fn replace_drinks(&self, chapter: &mut Chapter, dict: &Dict) -> Result<String, Error> {
|
||||
static RE: Lazy<Regex> = Lazy::new(|| {
|
||||
Regex::new(
|
||||
r"(?x) # insignificant whitespace mode
|
||||
r"(?x) # insignificant whitespace mode
|
||||
\{\{\s* # link opening parens and whitespace
|
||||
\#(drink) # drink marker
|
||||
\s+ # separating whitespace
|
||||
(?<drink>[A-z0-9_-]+) # drink name
|
||||
\}\} # link closing parens",
|
||||
).unwrap()
|
||||
)
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
static NODRINK: Lazy<String> = Lazy::new(|| {
|
||||
"deadbeef".to_string()
|
||||
});
|
||||
static NODRINK: Lazy<String> = Lazy::new(|| "deadbeef".to_string());
|
||||
|
||||
let res = RE.replace_all(&chapter.content, |caps: &Captures<'_>| {
|
||||
dict.get(&caps["drink"]).unwrap_or(&NODRINK)
|
||||
|
@ -59,9 +58,14 @@ impl Preprocessor for DrinkPreprocessor {
|
|||
|
||||
let drinks: Dict = {
|
||||
let reader = BufReader::new(File::open(path).expect("Cannot open drinks dictionary"));
|
||||
reader.lines().filter_map(|l| {
|
||||
l.expect("Cannot read line in drinks dictionary").split_once(SPLITTER).map(|(name, value)| (name.trim().to_owned(), value.trim().to_owned()))
|
||||
}).collect::<HashMap<_, _>>()
|
||||
reader
|
||||
.lines()
|
||||
.filter_map(|l| {
|
||||
l.expect("Cannot read line in drinks dictionary")
|
||||
.split_once(SPLITTER)
|
||||
.map(|(name, value)| (name.trim().to_owned(), value.trim().to_owned()))
|
||||
})
|
||||
.collect::<HashMap<_, _>>()
|
||||
};
|
||||
|
||||
book.for_each_mut(|section: &mut BookItem| {
|
||||
|
|
Loading…
Reference in New Issue