Fix formatting

This commit is contained in:
Andrey Voronkov 2024-02-28 21:26:13 +03:00
parent dacd1d16bc
commit 6f832ed434
2 changed files with 18 additions and 13 deletions

View File

@ -24,7 +24,8 @@ use topological_sort::TopologicalSort;
use crate::errors::*; use crate::errors::*;
use crate::preprocess::{ 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::renderer::{CmdRenderer, HtmlHandlebars, MarkdownRenderer, RenderContext, Renderer};
use crate::utils; use crate::utils;

View File

@ -2,15 +2,15 @@ use crate::errors::*;
use super::{Preprocessor, PreprocessorContext}; use super::{Preprocessor, PreprocessorContext};
use crate::book::{Book, BookItem, Chapter}; use crate::book::{Book, BookItem, Chapter};
use regex::{Captures, Regex};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::io::{BufRead, BufReader}; use regex::{Captures, Regex};
use std::fs::File;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
const SPLITTER: char = ':'; const SPLITTER: char = ':';
type Dict=HashMap<String, String>; type Dict = HashMap<String, String>;
/// DRY Links - A preprocessor for using centralized links collection: /// 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> { fn replace_drinks(&self, chapter: &mut Chapter, dict: &Dict) -> Result<String, Error> {
static RE: Lazy<Regex> = Lazy::new(|| { static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new( Regex::new(
r"(?x) # insignificant whitespace mode r"(?x) # insignificant whitespace mode
\{\{\s* # link opening parens and whitespace \{\{\s* # link opening parens and whitespace
\#(drink) # drink marker \#(drink) # drink marker
\s+ # separating whitespace \s+ # separating whitespace
(?<drink>[A-z0-9_-]+) # drink name (?<drink>[A-z0-9_-]+) # drink name
\}\} # link closing parens", \}\} # link closing parens",
).unwrap() )
.unwrap()
}); });
static NODRINK: Lazy<String> = Lazy::new(|| { static NODRINK: Lazy<String> = Lazy::new(|| "deadbeef".to_string());
"deadbeef".to_string()
});
let res = RE.replace_all(&chapter.content, |caps: &Captures<'_>| { let res = RE.replace_all(&chapter.content, |caps: &Captures<'_>| {
dict.get(&caps["drink"]).unwrap_or(&NODRINK) dict.get(&caps["drink"]).unwrap_or(&NODRINK)
@ -59,9 +58,14 @@ impl Preprocessor for DrinkPreprocessor {
let drinks: Dict = { let drinks: Dict = {
let reader = BufReader::new(File::open(path).expect("Cannot open drinks dictionary")); let reader = BufReader::new(File::open(path).expect("Cannot open drinks dictionary"));
reader.lines().filter_map(|l| { reader
l.expect("Cannot read line in drinks dictionary").split_once(SPLITTER).map(|(name, value)| (name.trim().to_owned(), value.trim().to_owned())) .lines()
}).collect::<HashMap<_, _>>() .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| { book.for_each_mut(|section: &mut BookItem| {