@@ -4,11 +4,23 @@ use std::{fmt::Display, path::Path};
4
4
5
5
use crate :: util;
6
6
7
+ /// A "section" is a piece of markdown that begins with `##` and which extends until the next section.
8
+ /// Note that we don't track the hierarchical structure of sections in particular.
7
9
#[ derive( Debug ) ]
8
10
pub struct Section {
11
+ /// Line numberin the document
9
12
pub line_num : usize ,
13
+
14
+ /// Number of hashes
15
+ pub level : usize ,
16
+
17
+ /// Title of the section -- what came after the `#` in the markdown.
10
18
pub title : String ,
19
+
20
+ /// Markdown text until start of next section, excluding tables
11
21
pub text : String ,
22
+
23
+ /// Tables are parsed and stored here
12
24
pub tables : Vec < Table > ,
13
25
}
14
26
@@ -30,10 +42,11 @@ pub fn parse(path: &Path) -> anyhow::Result<Vec<Section>> {
30
42
// eprintln!("line = {:?}", line);
31
43
// eprintln!("categorized = {:?}", categorized);
32
44
match categorized {
33
- CategorizeLine :: Title ( title) => {
45
+ CategorizeLine :: Title ( level , title) => {
34
46
close_section ( & mut result, & mut open_section, & mut open_table) ;
35
47
open_section = Some ( Section {
36
48
line_num,
49
+ level,
37
50
title,
38
51
text : String :: new ( ) ,
39
52
tables : vec ! [ ] ,
@@ -131,15 +144,16 @@ fn close_section(
131
144
132
145
#[ derive( Debug ) ]
133
146
enum CategorizeLine {
134
- Title ( String ) ,
147
+ Title ( usize , String ) ,
135
148
TableRow ( Vec < String > ) ,
136
149
TableDashRow ( usize ) ,
137
150
Other ,
138
151
}
139
152
140
153
fn categorize_line ( line : & str ) -> CategorizeLine {
141
154
if line. starts_with ( '#' ) {
142
- CategorizeLine :: Title ( line. trim_start_matches ( '#' ) . trim ( ) . to_string ( ) )
155
+ let level = line. chars ( ) . take_while ( |& ch| ch == '#' ) . count ( ) ;
156
+ CategorizeLine :: Title ( level, line. trim_start_matches ( '#' ) . trim ( ) . to_string ( ) )
143
157
} else if line. starts_with ( '|' ) && line. ends_with ( '|' ) {
144
158
let line = & line[ 1 ..line. len ( ) - 1 ] ;
145
159
let columns = line. split ( '|' ) . map ( |s| s. trim ( ) ) ;
0 commit comments