@ -17,26 +17,26 @@ lazy_static! {
// r"(?x)\\\{\{\#.*\}\}|\{\{\s*\#(template)\s+([\S]+)\s*\}\}|\{\{\s*\#(template)\s+([\S]+)\s+([^}]+)\}\}"
// r"(?x)\\\{\{\#.*\}\}|\{\{\s*\#(template)\s+([\S]+)\s*\}\}|\{\{\s*\#(template)\s+([\S]+)\s+([^}]+)\}\}"
static ref TEMPLATE : Regex = Regex ::new (
static ref TEMPLATE : Regex = Regex ::new (
r " ( ? x ) # enable insignificant whitespace mode
r " ( ? x ) # enable insignificant whitespace mode
\ \ \ { \ { # escaped link opening parens
\ \ \ { \ { # escaped link opening parens
\ #. * # match any character
\ #. * # match any character
\ } \ } # escaped link closing parens
\ } \ } # escaped link closing parens
| # or
| # or
\ { \ { \ s * # link opening parens and whitespace ( s )
\ { \ { \ s * # link opening parens and whitespace ( s )
\ #( template ) # link type - template
\ #( template ) # link type - template
\ s + # separating whitespace
\ s + # separating whitespace
( [ \ S ] + ) # relative path to template file
( [ \ S ] + ) # relative path to template file
\ s * # optional separating whitespaces ( s )
\ s * # optional separating whitespaces ( s )
\ } \ } # link closing parens
\ } \ } # link closing parens
| # or
| # or
\ { \ { \ s * # link opening parens and whitespace ( s )
\ { \ { \ s * # link opening parens and whitespace ( s )
\ #( template ) # link type - template
\ #( template ) # link type - template
\ s + # separating whitespace
\ s + # separating whitespace
( [ \ S ] + ) # relative path to template file
( [ \ S ] + ) # relative path to template file
\ s + # separating whitespace ( s )
\ s + # separating whitespace ( s )
( [ ^ } ] + ) # get all template arguments
( [ ^ } ] + ) # get all template arguments
\ } \ } # link closing parens "
\ } \ } # link closing parens "
@ -46,20 +46,20 @@ lazy_static! {
// r"(?x)\\\[\[.*\]\]|\[\[\s*\#([\S]+)\s*\]\]|\[\[\s*\#([\S]+)\s+([^]]+)\]\]"
// r"(?x)\\\[\[.*\]\]|\[\[\s*\#([\S]+)\s*\]\]|\[\[\s*\#([\S]+)\s+([^]]+)\]\]"
static ref ARGS : Regex = Regex ::new (
static ref ARGS : Regex = Regex ::new (
r " ( ? x ) # enable insignificant whitespace mode
r " ( ? x ) # enable insignificant whitespace mode
\ \ \ [ \ [ # escaped link opening square brackets
\ \ \ [ \ [ # escaped link opening square brackets
\ #. * # match any character
\ #. * # match any character
\ ] \ ] # escaped link closing parens
\ ] \ ] # escaped link closing parens
| # or
| # or
\ [ \ [ \ s * # link opening parens and whitespace ( s )
\ [ \ [ \ s * # link opening parens and whitespace ( s )
\ #( [ \ S ] + ) # arg name
\ #( [ \ S ] + ) # arg name
\ s * # optional separating whitespace ( s )
\ s * # optional separating whitespace ( s )
\ ] \ ] # link closing parens
\ ] \ ] # link closing parens
| # or
| # or
\ [ \ [ \ s * # link opening parens and whitespace ( s )
\ [ \ [ \ s * # link opening parens and whitespace ( s )
\ #( [ \ S ] + ) # arg name
\ #( [ \ S ] + ) # arg name
\ s + # optional separating whitespace ( s )
\ s + # optional separating whitespace ( s )
@ -113,23 +113,39 @@ impl<'a> Link<'a> {
. split ( LINE_BREAKS )
. split ( LINE_BREAKS )
. map ( | str | str . trim ( ) )
. map ( | str | str . trim ( ) )
. filter ( | trimmed | ! trimmed . is_empty ( ) )
. filter ( | trimmed | ! trimmed . is_empty ( ) )
. map( | mat | {
. filter_ map( | mat | {
let mut split_n = mat . splitn ( 2 , '=' ) ;
let mut split_n = mat . splitn ( 2 , '=' ) ;
let key = split_n . next ( ) . unwrap ( ) . trim ( ) ;
if let Some ( key ) = split_n . next ( ) {
let value = split_n . next ( ) . unwrap ( ) ;
let key = key . trim ( ) ;
( key , value )
if let Some ( value ) = split_n . next ( ) {
return Some ( ( key , value ) ) ;
}
}
eprintln! (
"Couldn't find a key/value pair while parsing the argument '{}'" ,
mat
) ;
None
} )
} )
. collect ::< Vec < _ > > ( ) ,
. collect ::< Vec < _ > > ( ) ,
// This looks like {{#template <file> <args>}}
// This looks like {{#template <file> <args>}}
false = > TEMPLATE_ARGS
false = > TEMPLATE_ARGS
. captures_iter ( args . as_str ( ) )
. captures_iter ( args . as_str ( ) )
. into_iter ( )
. filter_map ( | mat | {
. map ( | mat | {
let captures = mat . ok ( ) ? ;
let mut split_n = mat . unwrap ( ) . get ( 0 ) . unwrap ( ) . as_str ( ) . splitn ( 2 , '=' ) ;
let mut split_n = captures . get ( 0 ) ? . as_str ( ) . splitn ( 2 , '=' ) ;
let key = split_n . next ( ) . unwrap ( ) . trim ( ) ;
if let Some ( key ) = split_n . next ( ) {
let value = split_n . next ( ) . unwrap ( ) ;
let key = key . trim ( ) ;
( key , value )
if let Some ( value ) = split_n . next ( ) {
return Some ( ( key . trim ( ) , value ) ) ;
}
}
eprintln! (
"Couldn't parse key or value while parsing '{:?}'" ,
& args . as_str ( )
) ;
None
} )
} )
. collect ::< Vec < _ > > ( ) ,
. collect ::< Vec < _ > > ( ) ,
} ;
} ;
@ -157,7 +173,7 @@ impl<'a> Link<'a> {
FR : FileReader ,
FR : FileReader ,
{
{
match self . link_type {
match self . link_type {
LinkType ::Escaped = > Ok ( ( & self . link_text [ 1 .. ] ) . to_owned ( ) ) ,
LinkType ::Escaped = > Ok ( ( self . link_text [ 1 .. ] ) . to_owned ( ) ) ,
LinkType ::Template ( ref pat ) = > {
LinkType ::Template ( ref pat ) = > {
let target = base . as_ref ( ) . join ( pat ) ;
let target = base . as_ref ( ) . join ( pat ) ;
let contents = file_reader . read_to_string ( & target , self . link_text ) ? ;
let contents = file_reader . read_to_string ( & target , self . link_text ) ? ;
@ -195,7 +211,7 @@ impl<'a> Iterator for LinkIter<'a> {
fn next ( & mut self ) -> Option < Self ::Item > {
fn next ( & mut self ) -> Option < Self ::Item > {
for cap in & mut self . 0 {
for cap in & mut self . 0 {
if let Some ( inc ) = Link ::from_capture ( cap . unwrap( ) ) {
if let Some ( inc ) = Link ::from_capture ( cap . ok( ) ? ) {
return Some ( inc ) ;
return Some ( inc ) ;
}
}
}
}
@ -499,11 +515,14 @@ year=2022
#[ test ]
#[ test ]
fn test_extract_template_links_with_newlines_malformed ( ) {
fn test_extract_template_links_with_newlines_malformed ( ) {
let s = " { { #template test . rs
let s = [
lang = rust
"{{#template test.rs \n" ,
year = 2022 } } " ;
" lang=rust\n" ,
" year=2022}}" ,
let res = extract_template_links ( s ) . collect ::< Vec < _ > > ( ) ;
]
. concat ( ) ;
let res = extract_template_links ( & s ) . collect ::< Vec < _ > > ( ) ;
assert_eq! (
assert_eq! (
res ,
res ,