infodoc

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

README

The Infodoc Parser

Go Reference

This Go module parses documents that comply with the Infodoc standard with Tula markup.

Spec

Since this is the first Infodoc parser/renderer, specifications are included to write your own. See spec.md for info. If you need to clarify something, feel free to reach out!

Note

The HTML renderer included with this project renders sections as <section> instead of <p> because using <p> had issues, most notably that <p> self-closes after the first element if it is a block (for example, <p><h1>title</h1> text</p> becomes <p><h1>title</h1></p> text</p>.) This may be semantically incorrect, but it is syntactically correct, which is more important in my opinion.

Example

my-file.idoc

#idoc
title: My title
summary: My summary
---
[b bold text] [i italic text] [b [i both]]

main.go

package main

import (
    "os"
    "fmt"
    "codeberg.org/midna/infodoc"
)

func main() {
    content, err := os.ReadFile("./my-file.idoc")
    if err != nil {
       panic(err)
    }
    // parsing a document looks like this:
    document := infodoc.Parse(string(content))
    // metadata is a map of strings corresponding to strings (map[string]string)
    fmt.Printf("Document title:     %s\n", document.Metadata["title"])
    fmt.Printf("Document summary:   %s\n", document.Metadata["summary"])
    // you can render the tula markup to HTML like this:
    err = os.WriteFile("my-file.html", []byte(infodoc.RenderHtml(document)), 0644)
    if err != nil {
        panic(err)
    } else {
        println("Wrote my-file.html!")
    }
}
Output

stdout

Document title:     My title
Document summary:   My summary
Wrote my-file.html!

my-file.html

<section><b>bold text</b> <i>italic text</i> <b><i>both</i></b></section>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderHtml

func RenderHtml(doc Document) string

RenderHtml - Render parsed document data as HTML.

Types

type Datum

type Datum struct {
	// special
	Name     string
	Argument string
	Children []Datum
	// Raw
	Raw     bool
	RawText string
}

Datum - A parsed formatting node, similar to a tag in HTML

func NewRawDatum

func NewRawDatum(content string) Datum

NewRawDatum - Generate a new Datum containing no elements, but Raw text.

func NewSpecialDatum

func NewSpecialDatum(name string, argument string) Datum

NewSpecialDatum - Generate a new Datum which can contain elements.

func (*Datum) HasArgument

func (d *Datum) HasArgument() bool

HasArgument - Returns true if this Datum has a non-empty Argument, false otherwise.

func (*Datum) InnerText

func (d *Datum) InnerText() string

InnerText - Get the text contained in this element.

func (*Datum) Staple

func (d *Datum) Staple(newChild Datum)

Staple - Append a new Datum to a special Datum's child list. Note that this will panic if it is attempted on a Raw Datum.

type Document

type Document struct {
	Metadata map[string]string
	Content  []Datum
}

Document - A collection of Metadata and top-level nodes.

func Parse

func Parse(document string) Document

Parse - Parse a document.

type Scanner

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

The Scanner scans strings.

func NewScanner

func NewScanner(document string) Scanner

func (*Scanner) ClampPosition

func (s *Scanner) ClampPosition(pos int) int

func (*Scanner) InBounds

func (s *Scanner) InBounds(n int) bool

InBounds Check if this scanner's position + n is in bounds.

func (*Scanner) MatchBrackets

func (s *Scanner) MatchBrackets(open string, close string, raw bool, canEscape bool) string

MatchBrackets consumes all characters from open to its matching instance of close. This function takes into account escapes and nesting (for things like parenthesis.)

func (*Scanner) MatchUntil

func (s *Scanner) MatchUntil(needles []string, canEscape bool) string

MatchUntil matches all characters until any element of an array is found or the end of the file.

func (*Scanner) MatchUntilNewline

func (s *Scanner) MatchUntilNewline(canEscape bool) string

MatchUntilNewline matches all characters until a newline (CRLF, CR, or LF) or EOF.

func (*Scanner) MatchUntilWhitespace

func (s *Scanner) MatchUntilWhitespace(canEscape bool) string

MatchUntilWhitespace matches all characters until whitespace (CRLF, CR, LF, tab or space) or EOF.

func (*Scanner) Nudge

func (s *Scanner) Nudge(n int)

Nudge Move this scanner's position forward (or backward) n characters.

func (*Scanner) Peek

func (s *Scanner) Peek(n int) string

Peek Get the next or previous n characters from position depending on if n is positive or negative.

func (*Scanner) SkipWhitespace

func (s *Scanner) SkipWhitespace() int

SkipWhitespace skips whitespace and returns the number of skipped newlines.

Jump to

Keyboard shortcuts

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