File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ use regex:: Regex ;
2+
3+ /// Remove LICENSE and VERSION(S) sections by skipping lines between those headings and the next heading or EOF.
4+ /// Also removes <detail> tags.
5+ pub fn apply_tldr ( input : & str ) -> String {
6+ let mut output = Vec :: new ( ) ;
7+ let mut skip = false ;
8+
9+ // Match any heading (with or without space) for LICENSE or VERSION(S)
10+ let tldr_section_re = Regex :: new ( r"(?i)^\s*#+\s*(license|version(s)?)\b" ) . unwrap ( ) ;
11+ // Match any heading (for ending the skip)
12+ let heading_re = Regex :: new ( r"^\s*#+" ) . unwrap ( ) ;
13+ // Match <detail> tags including start, end, and inline attributes
14+ let detail_tag_re = Regex :: new ( r"<[/]?detail.*?>" ) . unwrap ( ) ;
15+
16+ for line in input. lines ( ) {
17+ // Start skipping if we hit a LICENSE or VERSION(S) heading
18+ if !skip && tldr_section_re. is_match ( line) {
19+ skip = true ;
20+ continue ; // skip the heading line itself
21+ }
22+ // Stop skipping at the next heading (but do not skip the heading itself)
23+ if skip && heading_re. is_match ( line) {
24+ skip = false ;
25+ }
26+ if !skip {
27+ // Remove <detail> tags from the line
28+ let cleaned_line = detail_tag_re. replace_all ( line, "" ) . to_string ( ) ;
29+ output. push ( cleaned_line) ;
30+ }
31+ }
32+ output. join ( "\n " )
33+ }
You can’t perform that action at this time.
0 commit comments