content

package
v0.5.52 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: BSD-3-Clause Imports: 55 Imported by: 4 modules, 2 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.

README

Cogent Content

Cogent Content is a package for making content-focused apps and websites consisting of Markdown, HTML, and Cogent Core.

Code blocks

Code blocks marked as ```Go (or ```Goal, with Cogent Lab) are interactive examples: the page shows their code in an editor and runs it. core build, core run, core install, core pack and core generate compile them into the app (as contentcode_gen.go, see contentcode), so that they run without an interpreter; edited code runs in the mvm interpreter if the app includes it (cogentcore.org/core/mvmcore). See https://cogentcore.org/core/build#code-blocks-of-content-apps.

References

Wikilink styles for scientific source references are supported, using the csl Citation Style Language format, with one citation per wikilink:

  • [[@CiteKey]] generates a Parenthetical citation, e.g., for APA style it is like: "Smith & Jones, 1989", which should be put in parentheses along with any others, e.g., ([[@SmithJones89]]; [[@West05]]).

  • [[@^CiteKey]] generates a Narrative citation, where the citation serves as the subject of the sentence. For APA style this is: "Smith & Jones (1989)".

Documentation

Overview

Package content provides a system for making content-focused apps and websites consisting of Markdown, HTML, and Cogent Core.

Index

Constants

This section is empty.

Variables

View Source
var BindTextEditor func(ed *textcore.Editor, parent *core.Frame, language string)

BindTextEditor is a function set to cogentcore.org/core/mvmcore.BindTextEditor when importing mvmcore, which provides interactive editing functionality for Go code blocks in text editors.

View Source
var CodeSymbols = map[string]any{}

CodeSymbols are values of the app, by name, that its code blocks use, such as embedded files. Compiled code (see CompiledCode) uses them from the app's package directly; interpreters (see BindTextEditor) add them to the names that code can use without a package, when they are first needed.

View Source
var CompiledCode = map[string]CodeFunc{}

CompiledCode maps the CodeKey of the source of a code block to the function compiled from it at build time. A code block whose source has an entry here runs without an interpreter.

View Source
var CompiledGoal = map[string]*GoalPage{}

CompiledGoal maps the name of a page file (as in bcontent.Page.Filename) to its Goal code compiled into the app. When a page is shown, its Goal code blocks run without an interpreter as long as they match the compiled blocks, in order.

View Source
var Files fs.FS

Files is the source filesystem of the content of the app (the Content.Source that was set last), for the code of its pages to open data files in it by their path in the content, as in table.OpenFS(content.Files, "datasets/planets.csv", tensor.Comma).

View Source
var FormatCode = map[string]func(src string) (string, error){
	"Go": formatGo,
}

FormatCode are the functions that format the code of the code blocks of each language (such as "Go") when it is edited, as gofmt does for Go code, which is the default. Other packages can add other languages, as cogentcore.org/lab/mvmlab does Goal. A function returns an error for code that it cannot format, which is left as it is.

View Source
var GoalReplay []string

GoalReplay are the sources of the Goal code blocks of the current page that ran compiled before the first one that runs in the interpreter (because it was edited or not compiled). An interpreter that makes a new Goal interpreter for the page (see BindTextEditor) first runs these sources, without showing anything, and clears them, so that the interpreted code sees the variables of the blocks before it, as it would if the whole page had run in the interpreter.

View Source
var ImageCacheBytes = 64 << 20

ImageCacheBytes is the maximum total size in bytes of the decoded images that a Content keeps in the htmlcore.ImageCache of its Content.Context, so that revisiting a page does not decode its images again.

View Source
var NewPageInitFunc func()

NewPageInitFunc is called when a new page is just being rendered. This can do any necessary new-page initialization, e.g., [mvmcore.ResetGoalInterpreter]

View Source
var OfflineURL = ""

OfflineURL is the non-web base url, which can be set to allow docs to refer to this in frontmatter.

Functions

func AddCompiledCode added in v0.5.0

func AddCompiledCode(code map[string]CodeFunc)

AddCompiledCode adds the given compiled code blocks to CompiledCode. Generated code calls it.

func AddCompiledGoal added in v0.5.0

func AddCompiledGoal(pages map[string]*GoalPage)

AddCompiledGoal adds the given compiled pages to CompiledGoal. Generated code calls it.

func BindCode added in v0.5.0

func BindCode(ed *textcore.Editor, parent *core.Frame, language string)

BindCode runs the code of the given text editor in the given parent, and again whenever the code is edited. Code that was compiled into the app (see CompiledCode) runs directly. Other code, including edited code, runs in the interpreter of BindTextEditor if there is one (mvm, through cogentcore.org/core/mvmcore); otherwise an edit shows a message that this app can not run edited code. Goal code runs compiled while it matches the Goal code compiled for the page (see CompiledGoal).

func CodeKey added in v0.5.0

func CodeKey(src string) string

CodeKey returns the key of the given code block source in CompiledCode: a hash of the source without any whitespace, so that indentation (tabs or spaces) and blank lines do not matter. The generator in cogentcore.org/core/content/contentcode computes the same key.

func OpenHelp

func OpenHelp(ctx core.Widget, docs fs.FS)

OpenHelp opens the given documentation of the app in a window, in the context of the given widget, or raises the window if it is open already. The content does not use the URL of the web page, which is the app's.

func SetAppHelp

func SetAppHelp(docs fs.FS)

SetAppHelp sets the help of the app (core.AppHelp), from the Help item of the app menu, to open the given documentation of the app, whose home page is home.md, with OpenHelp.

func ShowCodeErrors added in v0.5.7

func ShowCodeErrors(ed *textcore.Editor, parent *core.Frame, err error)

ShowCodeErrors shows the given error of running the code of the given text editor, if non-nil, and otherwise clears the errors of the editor. The errors at a position in the code are marked in the editor (see textcore.Base.SetCodeErrors), whose tooltips are their messages, and the messages of all of the errors are shown in the given parent, below the output of the code.

Types

type CodeFunc added in v0.5.0

type CodeFunc func(b *core.Frame, out io.Writer)

CodeFunc is a Go code block compiled into the app at build time (see cogentcore.org/core/content/contentcode, which the core tool runs in core build and core generate). It runs the code with the given parent widget, which the code knows as b, and writes what the code prints to out.

type Content

type Content struct {
	core.Splits

	// Source is the source filesystem for the content.
	// It should be set using [Content.SetSource] or [Content.SetContent].
	Source fs.FS `set:"-"`

	// Context is the [htmlcore.Context] used to render the content,
	// which can be modified for things such as adding wikilink handlers.
	Context *htmlcore.Context `set:"-"`

	// References is a list of references used for generating citation text
	// for literature reference wikilinks in the format [[@CiteKey]].
	References *csl.KeyList
	// contains filtered or unexported fields
}

Content manages and displays the content of a set of pages.

func NewContent

func NewContent(parent ...tree.Node) *Content

NewContent returns a new Content with the given optional parent: Content manages and displays the content of a set of pages.

func (*Content) Init

func (ct *Content) Init()

func (*Content) LoadEachPage added in v0.3.36

func (ct *Content) LoadEachPage(after func()) error

LoadEachPage is a helper function to iterate through loading each page, which is used for updating the math cache.

func (*Content) MakeToolbar

func (ct *Content) MakeToolbar(p *tree.Plan)

MakeToolbar adds the standard toolbar buttons for the content. See Content.MakeToolbarPDF for the optional PDF button.

func (*Content) MakeToolbarPDF added in v0.3.13

func (ct *Content) MakeToolbarPDF(p *tree.Plan)

MakeToolbarPDF adds the PDF button to the toolbar. This is optional.

func (*Content) MakeToolbarSearch added in v0.3.16

func (ct *Content) MakeToolbarSearch(p *tree.Plan)

MakeToolbarSearch adds a within-page search button to the toolbar. This is optional.

func (*Content) MenuSearch

func (ct *Content) MenuSearch(items *[]core.ChooserItem)

func (*Content) Open

func (ct *Content) Open(url string) *Content

Open opens the page with the given URL and updates the display. If no pages correspond to the URL, it is opened in the default browser. This version is for programmatic use -- see also OpenEvent.

func (*Content) OpenEvent added in v0.3.16

func (ct *Content) OpenEvent(url string, e events.Event) *Content

OpenEvent opens the page with the given URL and updates the display. If no pages correspond to the URL, it is opened in the default browser. This version is for widget event cases, where the keyboard modifiers are used to control the way the page is opened: Ctrl/Meta = new tab.

func (*Content) PagePDF added in v0.3.13

func (ct *Content) PagePDF(path string) error

PagePDF generates a PDF of the current page, to given file path (directory). the page name is the file name.

func (*Content) PageRefs added in v0.3.13

func (ct *Content) PageRefs(page *bcontent.Page) *core.Frame

PageRefs returns a core.Frame with the contents of the references cited on the given page. if References is nil, or error, result will be nil.

func (*Content) RenderWidget added in v0.3.36

func (ct *Content) RenderWidget()

func (*Content) SetContent

func (ct *Content) SetContent(content fs.FS) *Content

SetContent is a helper function that calls Content.SetSource with the "content" subdirectory of the given filesystem.

func (*Content) SetReferences added in v0.3.12

func (t *Content) SetReferences(v *csl.KeyList) *Content

SetReferences sets the Content.References: References is a list of references used for generating citation text for literature reference wikilinks in the format [[@CiteKey]].

func (*Content) SetSource

func (ct *Content) SetSource(source fs.FS) *Content

SetSource sets the source filesystem for the content, which is also the Files of the app.

type GoalBlock added in v0.5.0

type GoalBlock func(b *core.Frame, out io.Writer) GoalBlock

GoalBlock is a Goal code block compiled into the app at build time (see cogentcore.org/core/content/cmd/contentcode). It runs the code with the given parent widget, which the code knows as b, writes what the code prints to out, and returns the next Goal code block of its page (nil after the last one). The next block runs in the scope of this one, so that it sees the variables and functions this one declares, as in the Goal interpreter, which runs all the Goal code of a page in one interpreter.

type GoalPage added in v0.5.0

type GoalPage struct {

	// Keys are the [CodeKey]s of the sources of the Goal code blocks of
	// the page, in order.
	Keys []string

	// Start returns the first code block of a new run of the page,
	// in a new scope.
	Start func() GoalBlock
}

GoalPage is the Goal code of a page compiled into the app.

type History added in v0.3.16

type History struct {
	// Index is the current index in the Records, which is typically the
	// current page as saved. Back decrements then returns that.
	Index int

	// Records is the list of saved locations.
	Records []*Location
}

History records the history of browsing locations, for back arrow navigation.

func (*History) Back added in v0.3.16

func (hs *History) Back() (*Location, bool)

Back decrements Index and returns that location. If already at the start, returns false.

func (*History) Forward added in v0.3.16

func (hs *History) Forward() (*Location, bool)

Forward returns next location and increments Index. returns false if already at end (returns end location).

func (*History) Save added in v0.3.16

func (hs *History) Save(lc *Location)

Save adds the given location after the current one, removing any locations after the current one, unless it is the same as the current one, in which case nothing changes and Index stays valid.

type Location added in v0.3.16

type Location struct {
	Page    *bcontent.Page
	Heading string
	URL     string
}

Location holds one location of browsing history.

func (*Location) Clone added in v0.3.16

func (lc *Location) Clone() *Location

func (*Location) Reset added in v0.3.16

func (lc *Location) Reset()

func (*Location) String added in v0.3.16

func (lc *Location) String() string

type MountFS

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

MountFS is a filesystem of content that combines a root filesystem with other filesystems mounted in it at URL prefixes, such as the documentation of several apps on one website (each also used for the help of its app with SetAppHelp). Make it with NewMountFS and MountFS.Mount, and use it as the source of a Content with Content.SetSource.

The pages of a mounted filesystem are at its prefix: its root page (with URL = "" in its front matter, as for the home.md of the help of an app) has the URL of the prefix itself (such as "docs/lists"), and its other pages have their URLs under it (such as "docs/lists/settings"). The name of the root page is its Title, or else the sentence case of the last element of the prefix ("Lists"). The other pages keep their names unless a page of the root filesystem or of an earlier mount has the same name, in which case the name of the mount's root page is added in parentheses ("Settings (Cogent Lists)"). Relative URLs of images and other resources in a mounted page are relative to the page, so that a mounted filesystem works the same as on its own.

func NewMountFS

func NewMountFS(root fs.FS) *MountFS

NewMountFS returns a new MountFS with the given root filesystem, which may be nil for a filesystem that only has mounts.

func (*MountFS) Mount

func (m *MountFS) Mount(prefix string, fsys fs.FS) *MountFS

Mount mounts the given filesystem at the given prefix, a slash-separated path such as "docs/lists" (see MountFS). It panics if the prefix is not a valid path of fs.FS other than ".", or if it is mounted already.

func (*MountFS) Open

func (m *MountFS) Open(name string) (fs.File, error)

Open opens the named file, in a mount if it is in one, and otherwise in the root filesystem. The directories that contain mounts have the entries of those mounts, in addition to any entries of the root filesystem.

func (*MountFS) ReadDir

func (m *MountFS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads the named directory, as with MountFS.Open.

type SettingsData added in v0.3.13

type SettingsData struct {
	PDF paginate.Options

	// SiteTitle is the title of the site, used in page headings and titles.
	SiteTitle string

	// PageSettings is a function that returns the settings data to use
	// for the current page. Can set custom parameters for different pages.
	// The default sets the PDF Header function to HeaderLeftPageNumber
	// with current page Title.
	PageSettings func(ct *Content, curPage *bcontent.Page) *SettingsData
}

SettingsData has settings parameters for content, including PDF rendering options.

var Settings SettingsData

Settings are the current settings for content rendering.

func (*SettingsData) Defaults added in v0.3.13

func (s *SettingsData) Defaults()

Directories

Path Synopsis
Package bcontent ("base content") provides base types and functions shared by both content and the core build tool for content.
Package bcontent ("base content") provides base types and functions shared by both content and the core build tool for content.
cmd
contentcode command
Command contentcode compiles the runnable Go and Goal code blocks (```Go, ```Goal) of the markdown files of a content app into the app (see cogentcore.org/core/content/contentcode).
Command contentcode compiles the runnable Go and Goal code blocks (```Go, ```Goal) of the markdown files of a content app into the app (see cogentcore.org/core/content/contentcode).
Package contentcode compiles the runnable Go and Goal code blocks (```Go, ```Goal) of the markdown files of a content app into the app: it writes a Go file to the app's package that defines a function for each code block and registers it in cogentcore.org/core/content.CompiledCode (Goal: cogentcore.org/core/content.CompiledGoal).
Package contentcode compiles the runnable Go and Goal code blocks (```Go, ```Goal) of the markdown files of a content app into the app: it writes a Go file to the app's package that defines a function for each code block and registers it in cogentcore.org/core/content.CompiledCode (Goal: cogentcore.org/core/content.CompiledGoal).
examples
basic command

Jump to

Keyboard shortcuts

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