Skip to content

Commit

Permalink
make HeadingComment public (#134)
Browse files Browse the repository at this point in the history
* make HeadingComment public

* clippy
  • Loading branch information
digama0 authored Sep 29, 2023
1 parent 81c83b8 commit 3957f78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,13 +1093,21 @@ impl SegmentSet {
}
}

pub(crate) struct HeadingComment {
pub(crate) header: Span,
pub(crate) content: Span,
/// A parsed heading comment.
#[derive(Debug, Clone, Copy)]
pub struct HeadingComment {
/// The header part of the heading comment (the text of the header)
pub header: Span,
/// The content part of the heading comment (descriptive text regarding the header)
pub content: Span,
}

impl HeadingComment {
pub(crate) fn parse(buf: &[u8], lvl: HeadingLevel, sp: Span) -> Option<Self> {
/// Parses a heading comment at the given span in a segment buffer,
/// with the specified heading level and span. Returns `None` if this is not a heading comment
/// or it is malformed.
#[must_use]
pub fn parse(buf: &[u8], lvl: HeadingLevel, sp: Span) -> Option<Self> {
lazy_static::lazy_static! {
static ref MAJOR_PART: Regex =
Regex::new(r"^[ \n]+#{4,}\n *([^\n]*)\n#{4,}\n").unwrap();
Expand All @@ -1125,7 +1133,9 @@ impl HeadingComment {
})
}

pub(crate) fn parse_mathbox_header(&self, buf: &[u8]) -> Option<Span> {
/// Parses a mathbox heading comment, returning the span of the author name.
#[must_use]
pub fn parse_mathbox_header(&self, buf: &[u8]) -> Option<Span> {
lazy_static::lazy_static! {
static ref MATHBOX_FOR: Regex = Regex::new(r"^Mathbox for (.*)$").unwrap();
}
Expand Down
13 changes: 11 additions & 2 deletions src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::{borrow::Cow, ops::Deref};

use crate::{
comment_parser::{CommentParser, Discouragements, ParentheticalIter},
parser::HeadingLevel,
parser::{HeadingComment, HeadingLevel},
segment::SegmentRef,
};

Expand Down Expand Up @@ -721,7 +721,16 @@ impl<'a> StatementRef<'a> {
/// in this comment statement.
#[must_use]
pub fn parentheticals(&self) -> ParentheticalIter<'a> {
ParentheticalIter::new(&self.segment().segment.buffer, self.comment_contents())
ParentheticalIter::new(&self.segment.segment.buffer, self.comment_contents())
}

/// Returns a `HeadingComment` object for a heading comment (if it is actually a heading).
#[must_use]
pub fn as_heading_comment(&self) -> Option<HeadingComment> {
let StatementType::HeadingComment(lvl) = self.statement_type() else {
return None;
};
HeadingComment::parse(&self.segment.buffer, lvl, self.comment_contents())
}
}

Expand Down

0 comments on commit 3957f78

Please sign in to comment.