Replace anchors with their values.
(source: &str, context: JinjaContext)
| 119 | |
| 120 | /// Replace anchors with their values. |
| 121 | pub fn post_process(source: &str, context: JinjaContext) -> String { |
| 122 | let mut res = String::new(); |
| 123 | |
| 124 | for stmt in context.header { |
| 125 | res += stmt; |
| 126 | res += "\n"; |
| 127 | } |
| 128 | |
| 129 | let re = Regex::new(&format!(r"{ANCHOR_PREFIX}\d+")).unwrap(); |
| 130 | |
| 131 | let mut last_index = 0; |
| 132 | for cap in re.captures_iter(source) { |
| 133 | let cap = cap.get(0).unwrap(); |
| 134 | let index = cap.start(); |
| 135 | let anchor_id = cap.as_str(); |
| 136 | |
| 137 | res += &source[last_index..index]; |
| 138 | res += context.anchor_map.get(anchor_id).unwrap_or(&anchor_id); |
| 139 | |
| 140 | last_index = index + anchor_id.len(); |
| 141 | } |
| 142 | res += &source[last_index..]; |
| 143 | |
| 144 | res |
| 145 | } |
| 146 | |
| 147 | #[cfg(test)] |
| 148 | mod test { |