parser

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package parser は Markdown 文書を goldmark で解析し、 見出し・ID・ID 参照を取り出す。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefinitionID

func DefinitionID(re *regexp.Regexp, text string) (id string, ok bool)

DefinitionID は見出しテキスト text の先頭が ID パターン re に一致するときだけ、 その ID を定義として返す。テキストの途中に現れる一致は本文中の参照であって 定義ではないため、re.FindString だけでは区別できない。

func IsSetextUnderline

func IsSetextUnderline(line string) bool

IsSetextUnderline は行が setext 見出しの下線かを返す。

func PrimaryLevels

func PrimaryLevels(docs []*Document) map[string]int

PrimaryLevels は文書ごとの主レベル(その文書に実在する ID 付き見出しの 最も浅いレベル)をパスをキーにして返す。ID を 1 つも持たない文書は含まない。

主レベルの見出しがその文書の主要素、それより深いレベルは主要素の細目にあたる。 必須セクションの検査単位と、対応表の行の単位がこれで決まる。 設定項目にせず実データから導くので、採番するレベルを増やしても宣言が増えない。

単位が文書タイプではなく**文書**なのは理由が 2 つある。同じタイプを複数ファイルへ 分けたとき、一方が H2、他方が H3 で書かれていても、後者の識別子が対応表の行から 落ちない。そして解析対象の集合に依らないので、全体を検証しても 1 ファイルだけを 検証しても同じ結果になる。親子の区別はもともと同一文書内の話なので、これで足りる。

Types

type Document

type Document struct {
	Path     string // 設定ファイル基準の相対パス
	Type     string // 文書タイプ(設定の files[].type)
	Spec     config.FileSpec
	Source   []byte
	Lines    []string   // 改行を除いた行
	Endings  []string   // 各行を終える改行コード(最終行に改行が無ければ空)
	Newline  string     // 行を足すときに使う改行コード("\n" または "\r\n")
	Headings []*Heading // 文書順のフラットな並び(親子は Heading.Children)
	Roots    []*Heading // 親を持たない見出し(見出し木の根)を文書順で保持する
	Refs     []Ref      // 文書内の全参照(見出しでの ID 定義は含まない)
	// Prose は本文の行(コードブロックを除き、行内のコード表記を落としたもの)。
	// 用語の照合と本文の検索が、参照の抽出と同じ判断を使うための共通の入力。
	Prose []ProseLine
}

Document は解析済みの 1 文書。

func (*Document) IDs

func (d *Document) IDs() []*Heading

IDs は ID を持つ ID 付き見出しを文書順に返す。

func (*Document) IsPrimary

func (d *Document) IsPrimary(h *Heading, levels map[string]int) bool

IsPrimary は見出しがその文書の主レベルにあるかを返す。 levels は PrimaryLevels の結果。

func (*Document) OwnerAt

func (d *Document) OwnerAt(line int) *Heading

OwnerAt は指定行を含む直近の ID 付き見出しを返す。どの節にも属さなければ nil。 参照の紐付けと本文検索の帰属が同じ規則を使うために公開している。

type Heading

type Heading struct {
	Level    int    // 見出しレベル(1..6)
	Text     string // 見出し全文("R-1: ユーザー認証")
	Title    string // ID を除いた表題("ユーザー認証")
	ID       string // 抽出した ID。無ければ空
	Line     int    // 1 始まりの行番号
	EndLine  int    // この見出しが支配するセクションの最終行(含む)
	HasBody  bool   // 直下に本文があるか(空セクション検出用)
	Refs     []Ref  // このセクション内で参照された ID
	Children []*Heading
}

Heading は見出し 1 件。ID パターンに一致した見出しだけ ID / Title が埋まる。

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser は設定で決まった ID パターンに従って文書を解析する。

func New

func New(cfg *config.Config) (*Parser, error)

New は設定中の全 ID パターンを使う Parser を返す。

func (*Parser) Parse

func (p *Parser) Parse(path string, src []byte) (*Document, error)

Parse はソースを解析する。path は文書タイプと ID パターンの決定に使う。

func (*Parser) ParseFile

func (p *Parser) ParseFile(path string) (*Document, error)

ParseFile はファイルを読み込んで解析する。

func (*Parser) ParseFiles

func (p *Parser) ParseFiles(paths []string) ([]*Document, error)

ParseFiles は複数ファイルを解析する。1 件でも失敗すればエラーを返す。

type ProseLine

type ProseLine struct {
	Text string
	No   int
}

ProseLine は本文の 1 行。No は 1 始まりの行番号。

type Ref

type Ref struct {
	ID    string
	Line  int
	Owner string // 参照が現れたセクションの ID。文書直下なら空
}

Ref は文書中の ID 参照 1 件。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL