@@ -2,9 +2,11 @@ mod fence;
22mod highlight;
33
44use std:: ops:: RangeInclusive ;
5+ use std:: path:: PathBuf ;
56
67use errors:: { bail, Result } ;
78use libs:: syntect:: util:: LinesWithEndings ;
9+ use utils:: fs:: read_file;
810
911use crate :: codeblock:: highlight:: SyntaxHighlighter ;
1012use config:: highlighting:: { resolve_syntax_and_theme, HighlightSource } ;
@@ -17,11 +19,15 @@ fn opening_html(
1719 pre_style : Option < String > ,
1820 pre_class : Option < String > ,
1921 line_numbers : bool ,
22+ enable_copy : bool ,
2023) -> String {
2124 let mut html = String :: from ( "<pre" ) ;
2225 if line_numbers {
2326 html. push_str ( " data-linenos" ) ;
2427 }
28+ if enable_copy {
29+ html. push_str ( " data-copy" ) ;
30+ }
2531 let mut classes = String :: new ( ) ;
2632
2733 if let Some ( lang) = language {
@@ -81,11 +87,12 @@ pub struct CodeBlock<'config> {
8187 line_number_start : usize ,
8288 highlight_lines : Vec < RangeInclusive < usize > > ,
8389 hide_lines : Vec < RangeInclusive < usize > > ,
90+ include : Option < String > ,
8491}
8592
8693impl < ' config > CodeBlock < ' config > {
8794 pub fn new < ' fence_info > (
88- fence : FenceSettings < ' fence_info > ,
95+ fence : & FenceSettings < ' fence_info > ,
8996 config : & ' config Config ,
9097 // path to the current file if there is one, to point where the error is
9198 path : Option < & ' config str > ,
@@ -112,19 +119,33 @@ impl<'config> CodeBlock<'config> {
112119 highlighter. pre_style ( ) ,
113120 highlighter. pre_class ( ) ,
114121 fence. line_numbers ,
122+ fence. enable_copy ,
115123 ) ;
116124 Ok ( (
117125 Self {
118126 highlighter,
119127 line_numbers : fence. line_numbers ,
120128 line_number_start : fence. line_number_start ,
121- highlight_lines : fence. highlight_lines ,
122- hide_lines : fence. hide_lines ,
129+ highlight_lines : fence. highlight_lines . clone ( ) ,
130+ hide_lines : fence. hide_lines . clone ( ) ,
131+ include : fence. include . map ( |s| s. to_string ( ) ) ,
123132 } ,
124133 html_start,
125134 ) )
126135 }
127136
137+ pub fn include ( & self , base : Option < & PathBuf > ) -> Option < String > {
138+ let path = base?. join ( self . include . as_ref ( ) ?) ;
139+ let res = read_file ( & path) ;
140+ match res {
141+ Ok ( content) => Some ( content) ,
142+ Err ( e) => {
143+ eprintln ! ( "Warning: Failed to include {:?}: {}" , path, e) ;
144+ None
145+ }
146+ }
147+ }
148+
128149 pub fn highlight ( & mut self , content : & str ) -> String {
129150 let mut buffer = String :: new ( ) ;
130151 let mark_style = self . highlighter . mark_style ( ) ;
0 commit comments