render

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Longman5Online = "LONGMAN5/Online"
	LongmanEasy    = "LONGMAN/Easy"
	OLD9           = "OLD9"
)

Source type constants shared across HTML and Markdown renderers.

Variables

View Source
var SeparatorClose = "}"
View Source
var SeparatorOpen = "{"

Functions

func IsElement

func IsElement(n *html.Node, ele string, class string) bool

IsElement reports whether n is an element with the given tag name, and optionally an exact class match when class != "".

func Markdownify added in v0.1.5

func Markdownify(info io.Reader) string

func ParseHTML

func ParseHTML(info io.Reader) string

func ParseMDX

func ParseMDX(info io.Reader, ft string) string

Types

type EntryHandler added in v0.6.3

type EntryHandler struct{}

EntryHandler rewrites <a href="entry://word[#frag]"> to a local /dict URL. The format query parameter is taken from ctx.LinkFormat (defaults to "html"). The fragment (if any) is preserved as a real URL hash after stripping the trailing __a suffix that dict anchor names carry but element IDs don't.

func (EntryHandler) HandleNode added in v0.6.3

func (EntryHandler) HandleNode(n *html.Node, ctx RenderContext) bool

type HTMLRender

type HTMLRender struct {
	Raw        string
	SourceType string
	// LinkFormat is passed to RenderContext.LinkFormat to control the format=
	// parameter in rewritten entry:// links. Defaults to "html" when empty.
	LinkFormat string
	// EntryFetcher, when set, allows handlers to fetch other entries at
	// render time (e.g. ShowImageHandler resolving big_pic cross-refs).
	EntryFetcher func(word string) string
}

HTMLRender renders raw MDX/online HTML into clean browser-ready HTML by applying all registered NodeHandlers in a single DOM walk.

func (*HTMLRender) Render

func (h *HTMLRender) Render() string

type ImgHandler added in v0.6.3

type ImgHandler struct{}

ImgHandler normalises <img> src and promotes embedded base64 data URIs.

Priority order:

  1. base64 attribute present and is a data URI → use as src
  2. src has file:// prefix → strip the scheme
  3. src is relative (no leading / or http) → prefix with /
  4. src already root-relative or http → leave unchanged

func (ImgHandler) HandleNode added in v0.6.3

func (ImgHandler) HandleNode(n *html.Node, ctx RenderContext) bool

type MarkdownRender added in v0.1.5

type MarkdownRender struct {
	Raw        string
	SourceType string
}

func (*MarkdownRender) Render added in v0.1.5

func (m *MarkdownRender) Render() string

type NodeHandler added in v0.6.3

type NodeHandler interface {
	// HandleNode is called for every element node during DFS.
	// Returns true if children should be skipped.
	HandleNode(n *html.Node, ctx RenderContext) (skipChildren bool)
}

NodeHandler rewrites a single DOM node in-place. It is called during the DFS walk for every element node. Implementations should inspect the node and its attributes and mutate as needed. Returning true means the node was handled and the walker should NOT recurse into its children; false means the walker should continue recursing normally.

Note: returning false is the right choice when the handler mutates the node but children still need to be visited (e.g. sound:// turns <a> into <span> but the children <img> still need their src fixed).

type RenderContext added in v0.6.3

type RenderContext struct {
	// SourceType identifies the dictionary type (e.g. LongmanEasy, Longman5Online).
	SourceType string

	// LinkFormat is the value used for the "format" query parameter in
	// entry:// cross-reference links rewritten by EntryHandler.
	// Defaults to "html" when empty. Pass "html_fragment" when rendering
	// inside an already-framed page, or leave empty for the standard page.
	// Pass "raw" to skip rewriting entirely — entry:// links are left as-is
	// for native shells (e.g. Android) that handle them directly.
	LinkFormat string

	// EntryFetcher, when set, is called by handlers that need to fetch another
	// entry's rendered HTML (e.g. ShowImageHandler fetching big_pic from a
	// cross-referenced word). The function receives a plain word (no fragment)
	// and returns the rendered HTML string.
	EntryFetcher func(word string) string
}

RenderContext carries per-render metadata available to all NodeHandlers.

type Renderer

type Renderer interface {
	Render() string
}

Renderer is the common interface for all rendering backends.

type ShowImageHandler added in v0.6.3

type ShowImageHandler struct{}

ShowImageHandler resolves <a class="ldoce-show-image" base64="key123jpg"> cross-references that point to an illustration living in another entry.

Resolution strategy:

  1. Look for a sibling "see picture at WORD" link within the same .Sense span. Fetch that entry (using ctx.EntryFetcher), find the first .big_pic img, and rewrite the <a> href to that image path so the browser can open it.
  2. Fall back to deriving a filename from the base64 key (e.g. "ldoce4188jpg" → "ldoce4188.jpg") and using /filename directly. If the entryFetcher is nil (non-HTML context), just leave the element as-is.

The <a> is converted to a button-style span so it no longer carries a javascript:void(0) href; the JS lightbox in dict.html opens the image on click via the data-img-src attribute.

func (ShowImageHandler) HandleNode added in v0.6.3

func (ShowImageHandler) HandleNode(n *html.Node, ctx RenderContext) bool

type SoundHandler added in v0.6.3

type SoundHandler struct{}

SoundHandler wires audio playback for two raw MDX patterns:

  1. href="sound://file.mp3" — present in LDOCE5++ and Longman Easy for all speaker elements (headword and example). For online sources the href is rewritten to a plain /path; for MDX sources the element is converted to a data-audio-src trigger (see convertToAudioTrigger).

  2. data-src-mp3="/path/to.mp3" with no href — present in LDOCE5++ for inline example audio spans like: <span class="speaker exafile fa fa-volume-up" data-src-mp3="/media/..."> These have no href at all so case 1 misses them entirely. We detect them by the presence of data-src-mp3 when no sound:// href was found, and apply the same data-audio-src conversion.

func (SoundHandler) HandleNode added in v0.6.3

func (SoundHandler) HandleNode(n *html.Node, ctx RenderContext) bool

Jump to

Keyboard shortcuts

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