@@ -157,6 +157,40 @@ impl<'a> RfdAsciidoc<'a> {
157
157
fn body ( content : & str ) -> Option < & str > {
158
158
Self :: title_pattern ( ) . splitn ( content, 2 ) . nth ( 1 )
159
159
}
160
+
161
+ fn include_pattern ( ) -> Regex {
162
+ Regex :: new ( r"(?m)^include::(.*)\[\]$" ) . unwrap ( )
163
+ }
164
+
165
+ pub fn includes ( & ' a self ) -> Vec < AsciidocInclude < ' a > > {
166
+ Self :: include_pattern ( )
167
+ . captures_iter ( & self . resolved )
168
+ . map ( |capture| {
169
+ let extracted = capture. extract :: < 1 > ( ) ;
170
+ AsciidocInclude {
171
+ file : extracted. 1 [ 0 ] ,
172
+ replacement : extracted. 0 ,
173
+ }
174
+ } )
175
+ . collect :: < Vec < _ > > ( )
176
+ }
177
+ }
178
+
179
+ #[ derive( Debug , PartialEq ) ]
180
+ pub struct AsciidocInclude < ' a > {
181
+ file : & ' a str ,
182
+ replacement : & ' a str ,
183
+ }
184
+
185
+ impl < ' a > AsciidocInclude < ' a > {
186
+ pub fn name ( & self ) -> & str {
187
+ self . file
188
+ }
189
+
190
+ pub fn perform_replacement ( & self , body : & str , new_content : & str ) -> String {
191
+ tracing:: trace!( self . replacement, "Replacing include" ) ;
192
+ body. replace ( self . replacement , new_content)
193
+ }
160
194
}
161
195
162
196
impl < ' a > RfdDocument for RfdAsciidoc < ' a > {
@@ -197,36 +231,6 @@ impl<'a> RfdDocument for RfdAsciidoc<'a> {
197
231
198
232
fn get_authors ( & self ) -> Option < & str > {
199
233
Self :: attr ( "authors" , & self . resolved )
200
- // // If an authors attribute is defined anywhere in the document, then it is the first choice
201
- // // for the authors value
202
- // if let Some(attr) = Self::attr("authors", &self.resolved) {
203
- // Some(attr)
204
- // } else {
205
- // self.body().and_then(|body| {
206
- // body.lines().nth(0).and_then(|first_line| {
207
- // // If {authors} is found, instead search the header for an authors attribute
208
- // if first_line == "{authors}" {
209
- // Self::attr("authors", &self.resolved)
210
- // } else {
211
- // // Given that we are in a fallback case we need to be slightly picky on what
212
- // // lines we allow. We require that the line at least include a *@*.* word to
213
- // // try and filter out lines that are not actually author lines
214
- // let author_fallback_pattern =
215
- // Regex::new(r"^.*?([\S]+@[\S]+.[\S]+).*?$").unwrap();
216
- // let fallback_matches = author_fallback_pattern.is_match(first_line);
217
-
218
- // if fallback_matches {
219
- // Some(first_line)
220
- // } else {
221
- // // If none of our attempts have found an author, we drop back to the
222
- // // attribute lookup. Eventually all of this logic should be removed and only
223
- // // the attribute version should be supported
224
- // Self::attr("authors", &self.resolved)
225
- // }
226
- // }
227
- // })
228
- // })
229
- // }
230
234
}
231
235
232
236
fn get_labels ( & self ) -> Option < & str > {
@@ -970,4 +974,102 @@ This is the new body"#;
970
974
rfd. update_body ( & new_content) . unwrap ( ) ;
971
975
assert_eq ! ( expected, rfd. raw( ) ) ;
972
976
}
977
+
978
+ #[ test]
979
+ fn test_find_includes ( ) {
980
+ let contents = r#":reproducible:
981
+ :showtitle:
982
+ :toc: left
983
+ :numbered:
984
+ :icons: font
985
+ :state: prediscussion
986
+ :revremark: State: {state}
987
+ :docdatetime: 2019-01-04 19:26:06 UTC
988
+ :localdatetime: 2019-01-04 19:26:06 UTC
989
+ :labels: label1, label2
990
+
991
+ = RFD 123 Place
992
+ FirstName LastName <fname@company.org>
993
+
994
+ include::sub_doc1.adoc[]
995
+ This is the new body
996
+
997
+ include::sub_doc2.adoc[]"# ;
998
+ let rfd = RfdAsciidoc :: new ( Cow :: Borrowed ( contents) ) . unwrap ( ) ;
999
+ let includes = rfd. includes ( ) ;
1000
+
1001
+ let expected = vec ! [
1002
+ AsciidocInclude {
1003
+ file: "sub_doc1.adoc" ,
1004
+ replacement: "include::sub_doc1.adoc[]" ,
1005
+ } ,
1006
+ AsciidocInclude {
1007
+ file: "sub_doc2.adoc" ,
1008
+ replacement: "include::sub_doc2.adoc[]" ,
1009
+ } ,
1010
+ ] ;
1011
+ assert_eq ! ( expected, includes) ;
1012
+ }
1013
+
1014
+ #[ test]
1015
+ fn test_replace_include ( ) {
1016
+ let original = r#":reproducible:
1017
+ :showtitle:
1018
+ :toc: left
1019
+ :numbered:
1020
+ :icons: font
1021
+ :state: prediscussion
1022
+ :revremark: State: {state}
1023
+ :docdatetime: 2019-01-04 19:26:06 UTC
1024
+ :localdatetime: 2019-01-04 19:26:06 UTC
1025
+ :labels: label1, label2
1026
+
1027
+ = RFD 123 Place
1028
+ FirstName LastName <fname@company.org>
1029
+
1030
+ include::sub_doc1.adoc[]
1031
+ This is the new body
1032
+
1033
+ include::sub_doc1.adoc[]
1034
+
1035
+ include::sub_doc2.adoc[]"# ;
1036
+ let include = AsciidocInclude {
1037
+ file : "sub_doc1.adoc" ,
1038
+ replacement : "include::sub_doc1.adoc[]" ,
1039
+ } ;
1040
+
1041
+ let replacement_content = "Line 1
1042
+ Line 2
1043
+ Line 3" ;
1044
+
1045
+ let expected = r#":reproducible:
1046
+ :showtitle:
1047
+ :toc: left
1048
+ :numbered:
1049
+ :icons: font
1050
+ :state: prediscussion
1051
+ :revremark: State: {state}
1052
+ :docdatetime: 2019-01-04 19:26:06 UTC
1053
+ :localdatetime: 2019-01-04 19:26:06 UTC
1054
+ :labels: label1, label2
1055
+
1056
+ = RFD 123 Place
1057
+ FirstName LastName <fname@company.org>
1058
+
1059
+ Line 1
1060
+ Line 2
1061
+ Line 3
1062
+ This is the new body
1063
+
1064
+ Line 1
1065
+ Line 2
1066
+ Line 3
1067
+
1068
+ include::sub_doc2.adoc[]"# ;
1069
+
1070
+ assert_eq ! (
1071
+ expected,
1072
+ include. perform_replacement( original, replacement_content)
1073
+ ) ;
1074
+ }
973
1075
}
0 commit comments