Skip to content

Commit 7b77f9b

Browse files
author
Niko Matsakis
committed
track the indentation level for section headers
1 parent 7af4b89 commit 7b77f9b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

crates/rust-project-goals/src/markwaydown.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ use std::{fmt::Display, path::Path};
44

55
use crate::util;
66

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.
79
#[derive(Debug)]
810
pub struct Section {
11+
/// Line numberin the document
912
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.
1018
pub title: String,
19+
20+
/// Markdown text until start of next section, excluding tables
1121
pub text: String,
22+
23+
/// Tables are parsed and stored here
1224
pub tables: Vec<Table>,
1325
}
1426

@@ -30,10 +42,11 @@ pub fn parse(path: &Path) -> anyhow::Result<Vec<Section>> {
3042
// eprintln!("line = {:?}", line);
3143
// eprintln!("categorized = {:?}", categorized);
3244
match categorized {
33-
CategorizeLine::Title(title) => {
45+
CategorizeLine::Title(level, title) => {
3446
close_section(&mut result, &mut open_section, &mut open_table);
3547
open_section = Some(Section {
3648
line_num,
49+
level,
3750
title,
3851
text: String::new(),
3952
tables: vec![],
@@ -131,15 +144,16 @@ fn close_section(
131144

132145
#[derive(Debug)]
133146
enum CategorizeLine {
134-
Title(String),
147+
Title(usize, String),
135148
TableRow(Vec<String>),
136149
TableDashRow(usize),
137150
Other,
138151
}
139152

140153
fn categorize_line(line: &str) -> CategorizeLine {
141154
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())
143157
} else if line.starts_with('|') && line.ends_with('|') {
144158
let line = &line[1..line.len() - 1];
145159
let columns = line.split('|').map(|s| s.trim());

0 commit comments

Comments
 (0)