htmlmd

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: GPL-3.0 Imports: 19 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 htmlmd renders a dictionary record's own HTML as Markdown.

It is the one presentation that reads entry HTML rather than the Entry IR:

EntrySet → bobadapter / textrender / mdrender
record HTML + dictionary CSS → htmlmd → Markdown

The IR presentations show what the parser understood, in a layout this project chose. This one shows the page the dictionary's editors laid out, which is what a reader of a richly typeset dictionary usually wants once Bob can draw Markdown. The two answer different questions, so neither replaces the other and neither reads the other's output.

The general HTML-to-Markdown work — whitespace collapsing, escaping, lists, tables, emphasis — is done by github.com/JohannesKaufmann/html-to-markdown. What this package adds is what a generic converter cannot know about MDict records: that layout lives in the stylesheet rather than in the tags, that `sound://` and `entry://` are not web links, and that images and audio live in the dictionary's MDD.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(raw []byte, opts Options) string

Convert renders one record. It never fails: a record the converter cannot make sense of comes out as its visible text.

func Link(text, target string) string

Link writes a Markdown link, escaping what its text could break.

func RenderRecords

func RenderRecords(records []Record, opts RecordOptions) string

RenderRecords renders the records under one key with the record boundaries and navigation of the IR Markdown, so switching views never changes how several records are presented (selectors become lookup links when Options.LookupLink provides them):

  • combined: each record under a "Record n of total" heading, divided by a thematic break;
  • separate: one record, then "Other entries" listing its siblings' selectors as copyable query text.

The navigation is added even when the dictionary links its homographs itself: many do not, and a record reached through a redirect never shows the siblings it was merged with. Unlike the IR view there is no title heading — the dictionary's page already prints its headword. Only records that are shown are converted.

func Selector

func Selector(key string, ordinal int) string

Selector is the reserved query form that names one record: the key with the record ordinal in superscript.

func StylesheetLinks(doc *html.Node, visit func(href string))

StylesheetLinks calls visit with the href of every stylesheet a document links to, in document order.

Types

type MultiRecordMode

type MultiRecordMode string

MultiRecordMode mirrors the option of the same name in the IR renderers: show one record and how to reach the others, or show them all.

const (
	MultiRecordCombined MultiRecordMode = "combined"
	MultiRecordSeparate MultiRecordMode = "separate"
)

type Options

type Options struct {
	Resources Resources
	// Stylesheets apply after the record's own, so they win ties. A
	// dictionary profile uses them to hide interface chrome.
	Stylesheets []*Stylesheet
	// Scope narrows the parsed document before conversion — a dictionary
	// profile's entry root. It receives the document after the cascade and
	// returns the node to convert.
	Scope func(doc *html.Node) *html.Node
	// LookupLink turns a word a dictionary links to into a URL that looks it
	// up in Bob, or returns "" when it cannot. Nil, or "", leaves the link as
	// plain text.
	LookupLink func(query string) string
}

Options configures one conversion.

type Record

type Record struct {
	Ordinal int
	HTML    []byte
}

Record is one visible record under a key: its ordinal in the EntrySet, so selectors mean the same record in every presentation, and its HTML.

type RecordOptions

type RecordOptions struct {
	Options
	// Key is the lookup key sibling selectors are built from.
	Key string
	// Total is how many records the key has, for "Record n of total"; zero
	// means len(records).
	Total           int
	MultiRecordMode MultiRecordMode
	// RecordOrdinal selects one record; non-zero always wins over the mode.
	RecordOrdinal int
}

RecordOptions configures RenderRecords.

type Resources

type Resources interface {
	// Stylesheet returns the parsed stylesheet a <link> names, or nil.
	Stylesheet(href string) *Stylesheet
	// Audio returns a playable URL for a sound reference.
	Audio(ref string) string
	// Image returns a displayable URL for an image reference.
	Image(ref string) string
}

Resources resolves what a record refers to. Every method may return an empty value, which means "not available": the reference is then dropped rather than emitted as a link that would not work.

type Stylesheet

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

Stylesheet is the part of a dictionary stylesheet that changes what a Markdown reader should see.

Dictionary HTML says almost nothing through its tags: a sense, an example and a translation are all <span>s, and the stylesheet is what turns them into separate lines, hides the language variant the reader did not pick, and bolds a label. Only the properties that decide that survive parsing — display, visibility, font weight and style, horizontal spacing, and the text of ::before and ::after with enough positioning to tell decoration from content. Colour, size and the rest of layout have no Markdown equivalent.

func ParseStylesheet

func ParseStylesheet(src []byte) *Stylesheet

ParseStylesheet reads a CSS file. Rules it cannot interpret — selectors cascadia does not support, conditional @media blocks, nested rules — are skipped rather than failing the sheet: one exotic selector in a 70 KB stylesheet must not cost the reader every other rule.

Jump to

Keyboard shortcuts

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