histyle

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: BSD-3-Clause Imports: 15 Imported by: 3

Documentation

Overview

package histyle provides syntax highlighting styles -- it interoperates with github.com/alecthomas/chroma which in turn interoperates with the python pygments package. Note that this package depends on goki/gi and cannot be imported there -- is imported into goki/gi/giv

Index

Constants

View Source
const (
	// ChromaTag is our starting tag -- anything above this is custom..
	ChromaTag chroma.TokenType = 10000 + iota

	// ChromaSpellErr tags a spelling error
	ChromaSpellErr
)

Chroma tag token types -- our extensions to the chroma token type

View Source
const (
	Whitespace = TextWhitespace

	Date = LiteralDate

	String          = LiteralString
	StringAffix     = LiteralStringAffix
	StringBacktick  = LiteralStringBacktick
	StringChar      = LiteralStringChar
	StringDelimiter = LiteralStringDelimiter
	StringDoc       = LiteralStringDoc
	StringDouble    = LiteralStringDouble
	StringEscape    = LiteralStringEscape
	StringHeredoc   = LiteralStringHeredoc
	StringInterpol  = LiteralStringInterpol
	StringOther     = LiteralStringOther
	StringRegex     = LiteralStringRegex
	StringSingle    = LiteralStringSingle
	StringSymbol    = LiteralStringSymbol

	Number            = LiteralNumber
	NumberBin         = LiteralNumberBin
	NumberFloat       = LiteralNumberFloat
	NumberHex         = LiteralNumberHex
	NumberInteger     = LiteralNumberInteger
	NumberIntegerLong = LiteralNumberIntegerLong
	NumberOct         = LiteralNumberOct
)

Aliases.

Variables

View Source
var ChromaTagNames = map[chroma.TokenType]string{
	ChromaSpellErr: "cse",
}

ChromaTagNames are our style names for Chroma tags -- need to ensure CSS exists for these

View Source
var ChromaToHiTagsMap map[chroma.TokenType]HiTags

ChromaToHiTagsMap maps from chroma.TokenType to HiTags -- built from opposite map

View Source
var CustomStyles = Styles{}

CustomStyles are user's special styles

View Source
var HiTagNames map[HiTags]string

HiTagNames is the complete mapping from a HiTags tag to the css style name most are 2 letters -- populated from chroma.StandardTypes plus custom

View Source
var HiTagsProps = map[HiTags]ki.Props{
	SpellErr: ki.Props{
		"text-decoration": 1 << uint32(gi.DecoDottedUnderline),
	},
}

HiTagsProps are default properties for custom tags -- if set in style then used there but otherwise we use these as a fallback -- typically not overridden

View Source
var HiTagsToChromaMap = map[HiTags]chroma.TokenType{
	EOFType:                  chroma.EOFType,
	Background:               chroma.Background,
	LineNumbers:              chroma.LineNumbers,
	LineNumbersTable:         chroma.LineNumbersTable,
	LineHighlight:            chroma.LineHighlight,
	LineTable:                chroma.LineTable,
	LineTableTD:              chroma.LineTableTD,
	Error:                    chroma.Error,
	Other:                    chroma.Other,
	None:                     chroma.None,
	Keyword:                  chroma.Keyword,
	KeywordConstant:          chroma.KeywordConstant,
	KeywordDeclaration:       chroma.KeywordDeclaration,
	KeywordNamespace:         chroma.KeywordNamespace,
	KeywordPseudo:            chroma.KeywordPseudo,
	KeywordReserved:          chroma.KeywordReserved,
	KeywordType:              chroma.KeywordType,
	Name:                     chroma.Name,
	NameAttribute:            chroma.NameAttribute,
	NameBuiltin:              chroma.NameBuiltin,
	NameBuiltinPseudo:        chroma.NameBuiltinPseudo,
	NameClass:                chroma.NameClass,
	NameConstant:             chroma.NameConstant,
	NameDecorator:            chroma.NameDecorator,
	NameEntity:               chroma.NameEntity,
	NameException:            chroma.NameException,
	NameFunction:             chroma.NameFunction,
	NameFunctionMagic:        chroma.NameFunctionMagic,
	NameKeyword:              chroma.NameKeyword,
	NameLabel:                chroma.NameLabel,
	NameNamespace:            chroma.NameNamespace,
	NameOperator:             chroma.NameOperator,
	NameOther:                chroma.NameOther,
	NamePseudo:               chroma.NamePseudo,
	NameProperty:             chroma.NameProperty,
	NameTag:                  chroma.NameTag,
	NameVariable:             chroma.NameVariable,
	NameVariableAnonymous:    chroma.NameVariableAnonymous,
	NameVariableClass:        chroma.NameVariableClass,
	NameVariableGlobal:       chroma.NameVariableGlobal,
	NameVariableInstance:     chroma.NameVariableInstance,
	NameVariableMagic:        chroma.NameVariableMagic,
	Literal:                  chroma.Literal,
	LiteralDate:              chroma.LiteralDate,
	LiteralOther:             chroma.LiteralOther,
	LiteralString:            chroma.LiteralString,
	LiteralStringAffix:       chroma.LiteralStringAffix,
	LiteralStringAtom:        chroma.LiteralStringAtom,
	LiteralStringBacktick:    chroma.LiteralStringBacktick,
	LiteralStringBoolean:     chroma.LiteralStringBoolean,
	LiteralStringChar:        chroma.LiteralStringChar,
	LiteralStringDelimiter:   chroma.LiteralStringDelimiter,
	LiteralStringDoc:         chroma.LiteralStringDoc,
	LiteralStringDouble:      chroma.LiteralStringDouble,
	LiteralStringEscape:      chroma.LiteralStringEscape,
	LiteralStringHeredoc:     chroma.LiteralStringHeredoc,
	LiteralStringInterpol:    chroma.LiteralStringInterpol,
	LiteralStringName:        chroma.LiteralStringName,
	LiteralStringOther:       chroma.LiteralStringOther,
	LiteralStringRegex:       chroma.LiteralStringRegex,
	LiteralStringSingle:      chroma.LiteralStringSingle,
	LiteralStringSymbol:      chroma.LiteralStringSymbol,
	LiteralNumber:            chroma.LiteralNumber,
	LiteralNumberBin:         chroma.LiteralNumberBin,
	LiteralNumberFloat:       chroma.LiteralNumberFloat,
	LiteralNumberHex:         chroma.LiteralNumberHex,
	LiteralNumberInteger:     chroma.LiteralNumberInteger,
	LiteralNumberIntegerLong: chroma.LiteralNumberIntegerLong,
	LiteralNumberOct:         chroma.LiteralNumberOct,
	Operator:                 chroma.Operator,
	OperatorWord:             chroma.OperatorWord,
	Punctuation:              chroma.Punctuation,
	Comment:                  chroma.Comment,
	CommentHashbang:          chroma.CommentHashbang,
	CommentMultiline:         chroma.CommentMultiline,
	CommentSingle:            chroma.CommentSingle,
	CommentSpecial:           chroma.CommentSpecial,
	CommentPreproc:           chroma.CommentPreproc,
	CommentPreprocFile:       chroma.CommentPreprocFile,
	Generic:                  chroma.Generic,
	GenericDeleted:           chroma.GenericDeleted,
	GenericEmph:              chroma.GenericEmph,
	GenericError:             chroma.GenericError,
	GenericHeading:           chroma.GenericHeading,
	GenericInserted:          chroma.GenericInserted,
	GenericOutput:            chroma.GenericOutput,
	GenericPrompt:            chroma.GenericPrompt,
	GenericStrong:            chroma.GenericStrong,
	GenericSubheading:        chroma.GenericSubheading,
	GenericTraceback:         chroma.GenericTraceback,
	GenericUnderline:         chroma.GenericUnderline,
	Text:                     chroma.Text,
	TextWhitespace:           chroma.TextWhitespace,
	TextSymbol:               chroma.TextSymbol,
	TextPunctuation:          chroma.TextPunctuation,
	SpellErr:                 ChromaSpellErr,
}

HiTagsToChromaMap maps from HiTags to chroma.TokenType

View Source
var KiT_HiTags = kit.Enums.AddEnumAltLower(HiTagsN, false, nil, "")
View Source
var KiT_Style = kit.Types.AddType(&Style{}, StyleProps)
View Source
var KiT_StyleEntry = kit.Types.AddType(&StyleEntry{}, StyleEntryProps)
View Source
var KiT_Styles = kit.Types.AddType(&Styles{}, StylesProps)
View Source
var KiT_Trilean = kit.Enums.AddEnumAltLower(TrileanN, false, nil, "")
View Source
var PrefsStylesFileName = "hi_styles.json"

PrefsStylesFileName is the name of the preferences file in App prefs directory for saving / loading the custom styles

View Source
var StyleDefault = StyleName("emacs")

StyleDefault is the default highlighting style name -- can set this to whatever you want

View Source
var StyleEntryProps = ki.Props{
	"inline": true,
}
View Source
var StyleNames []string

StyleNames are all the names of all the available highlighting styles

View Source
var StyleProps = ki.Props{
	"MainMenu": ki.PropSlice{
		{"AppMenu", ki.BlankProp{}},
		{"File", ki.PropSlice{
			{"OpenJSON", ki.Props{
				"label":    "Open from file",
				"desc":     "You can save and open styles to / from files to share, experiment, transfer, etc",
				"shortcut": gi.KeyFunMenuOpen,
				"Args": ki.PropSlice{
					{"File Name", ki.Props{
						"ext": ".json",
					}},
				},
			}},
			{"SaveJSON", ki.Props{
				"label":    "Save to file",
				"desc":     "You can save and open styles to / from files to share, experiment, transfer, etc",
				"shortcut": gi.KeyFunMenuSaveAs,
				"Args": ki.PropSlice{
					{"File Name", ki.Props{
						"ext": ".json",
					}},
				},
			}},
		}},
		{"Edit", "Copy Cut Paste Dupe"},
		{"Window", "Windows"},
	},
	"ToolBar": ki.PropSlice{
		{"OpenJSON", ki.Props{
			"label": "Open from file",
			"icon":  "file-open",
			"desc":  "You can save and open styles to / from files to share, experiment, transfer, etc -- save from standard ones and load into custom ones for example",
			"Args": ki.PropSlice{
				{"File Name", ki.Props{
					"ext": ".json",
				}},
			},
		}},
		{"SaveJSON", ki.Props{
			"label": "Save to file",
			"icon":  "file-save",
			"desc":  "You can save and open styles to / from files to share, experiment, transfer, etc -- save from standard ones and load into custom ones for example",
			"Args": ki.PropSlice{
				{"File Name", ki.Props{
					"ext": ".json",
				}},
			},
		}},
	},
}

StyleProps define the ToolBar and MenuBar for view

View Source
var StylesChanged = false

StylesChanged is used for gui updating while editing

View Source
var StylesProps = ki.Props{
	"MainMenu": ki.PropSlice{
		{"AppMenu", ki.BlankProp{}},
		{"File", ki.PropSlice{
			{"OpenPrefs", ki.Props{}},
			{"SavePrefs", ki.Props{
				"shortcut": gi.KeyFunMenuSave,
				"updtfunc": func(sti interface{}, act *gi.Action) {
					act.SetActiveStateUpdt(StylesChanged && sti.(*Styles) == &CustomStyles)
				},
			}},
			{"sep-file", ki.BlankProp{}},
			{"OpenJSON", ki.Props{
				"label":    "Open from file",
				"desc":     "You can save and open styles to / from files to share, experiment, transfer, etc",
				"shortcut": gi.KeyFunMenuOpen,
				"Args": ki.PropSlice{
					{"File Name", ki.Props{
						"ext": ".json",
					}},
				},
			}},
			{"SaveJSON", ki.Props{
				"label":    "Save to file",
				"desc":     "You can save and open styles to / from files to share, experiment, transfer, etc",
				"shortcut": gi.KeyFunMenuSaveAs,
				"Args": ki.PropSlice{
					{"File Name", ki.Props{
						"ext": ".json",
					}},
				},
			}},
		}},
		{"Edit", "Copy Cut Paste Dupe"},
		{"Window", "Windows"},
	},
	"ToolBar": ki.PropSlice{
		{"Add", ki.Props{
			"desc": "Add a new style to the list.",
			"icon": "plus",
			"updtfunc": func(sti interface{}, act *gi.Action) {
				act.SetActiveStateUpdt(sti.(*Styles) == &CustomStyles)
			},
		}},
		{"SavePrefs", ki.Props{
			"desc": "saves styles to app prefs directory, in file hi_styles.json, which will be loaded automatically at startup into your CustomStyles.",
			"icon": "file-save",
			"updtfunc": func(sti interface{}, act *gi.Action) {
				act.SetActiveStateUpdt(StylesChanged && sti.(*Styles) == &CustomStyles)
			},
		}},
		{"sep-file", ki.BlankProp{}},
		{"OpenJSON", ki.Props{
			"label": "Open from file",
			"icon":  "file-open",
			"desc":  "You can save and open styles to / from files to share, experiment, transfer, etc",
			"Args": ki.PropSlice{
				{"File Name", ki.Props{
					"ext": ".json",
				}},
			},
		}},
		{"SaveJSON", ki.Props{
			"label": "Save to file",
			"icon":  "file-save",
			"desc":  "You can save and open styles to / from files to share, experiment, transfer, etc",
			"Args": ki.PropSlice{
				{"File Name", ki.Props{
					"ext": ".json",
				}},
			},
		}},
		{"sep-std", ki.BlankProp{}},
		{"ViewStd", ki.Props{
			"desc":    `Shows the standard styles that are compiled into the program (from <a href="https://github.com/alecthomas/chroma">github.com/alecthomas/chroma</a>).  Save a style from there and load it into custom as a starting point for creating a variant of an existing style.`,
			"confirm": true,
			"updtfunc": func(sti interface{}, act *gi.Action) {
				act.SetActiveStateUpdt(sti.(*Styles) != &StdStyles)
			},
		}},
	},
}

StylesProps define the ToolBar and MenuBar for view

Functions

func Init

func Init()

Init must be called to initialize the hi styles -- post startup so chroma stuff is all in place, and loads custom styles

func InitHiTagNames

func InitHiTagNames()

InitHiTagNames initializes the HiTagNames -- called during overall init

func MergeAvailStyles

func MergeAvailStyles()

MergeAvailStyles updates AvailStyles as combination of std and custom styles

Types

type HiTags

type HiTags int

HiTags are highlighting tags -- one-to-one with chroma.Token but kept in simple sequential order for use as an enum -- with enums and the string interface the raw numbers don't matter so sequential is best

const (
	// Used as an EOF marker / nil token
	EOFType HiTags = iota
	// Default background style.
	Background
	// Line numbers in output.
	LineNumbers
	// Line numbers in output when in table.
	LineNumbersTable
	// Line higlight style.
	LineHighlight
	// Line numbers table wrapper style.
	LineTable
	// Line numbers table TD wrapper style.
	LineTableTD
	// Input that could not be tokenised.
	Error
	// Other is used by the Delegate lexer to indicate which tokens should be handled by the delegate.
	Other
	// No highlighting.
	None
	// Keywords.
	Keyword
	KeywordConstant
	KeywordDeclaration
	KeywordNamespace
	KeywordPseudo
	KeywordReserved
	KeywordType
	// Names.
	Name
	NameAttribute
	NameBuiltin
	NameBuiltinPseudo
	NameClass
	NameConstant
	NameDecorator
	NameEntity
	NameException
	NameFunction
	NameFunctionMagic
	NameKeyword
	NameLabel
	NameNamespace
	NameOperator
	NameOther
	NamePseudo
	NameProperty
	NameTag
	NameVariable
	NameVariableAnonymous
	NameVariableClass
	NameVariableGlobal
	NameVariableInstance
	NameVariableMagic
	// Literals.
	Literal
	LiteralDate
	LiteralOther
	// Literal Strings.
	LiteralString
	LiteralStringAffix
	LiteralStringAtom
	LiteralStringBacktick
	LiteralStringBoolean
	LiteralStringChar
	LiteralStringDelimiter
	LiteralStringDoc
	LiteralStringDouble
	LiteralStringEscape
	LiteralStringHeredoc
	LiteralStringInterpol
	LiteralStringName
	LiteralStringOther
	LiteralStringRegex
	LiteralStringSingle
	LiteralStringSymbol
	// Literals Numbers.
	LiteralNumber
	LiteralNumberBin
	LiteralNumberFloat
	LiteralNumberHex
	LiteralNumberInteger
	LiteralNumberIntegerLong
	LiteralNumberOct
	// Operators.
	Operator
	OperatorWord
	// Punctuation.
	Punctuation
	// Comments.
	Comment
	CommentHashbang
	CommentMultiline
	CommentSingle
	CommentSpecial
	// Preprocessor "comments".
	CommentPreproc
	CommentPreprocFile
	// Generic tokens.
	Generic
	GenericDeleted
	GenericEmph
	GenericError
	GenericHeading
	GenericInserted
	GenericOutput
	GenericPrompt
	GenericStrong
	GenericSubheading
	GenericTraceback
	GenericUnderline
	// Text.
	Text
	TextWhitespace
	TextSymbol
	TextPunctuation
	// Our own custom types
	SpellErr

	HiTagsN
)

HiTags values -- MUST keep this in correspondence with alecthomas/chroma for interoperability

func HiTagFromChroma

func HiTagFromChroma(ct chroma.TokenType) HiTags

HiTagFromChroma returns a HiTags tag from a chroma tag

func (HiTags) Category

func (ht HiTags) Category() HiTags

func (HiTags) ClassName

func (ht HiTags) ClassName() string

ClassName returns the . prefixed CSS classname of the tag style for styling, a CSS property should exist with this name

func (*HiTags) FromChroma

func (ht *HiTags) FromChroma(ct chroma.TokenType)

FromChroma converts a chroma.TokenType to a HiTags type

func (*HiTags) FromString

func (i *HiTags) FromString(s string) error

func (HiTags) InCategory

func (ht HiTags) InCategory(other HiTags) bool

func (HiTags) InSubCategory

func (ht HiTags) InSubCategory(other HiTags) bool

func (HiTags) MarshalJSON

func (ev HiTags) MarshalJSON() ([]byte, error)

func (HiTags) MarshalText

func (ev HiTags) MarshalText() ([]byte, error)

map keys require text marshaling:

func (HiTags) Parent

func (ht HiTags) Parent() HiTags

func (HiTags) String

func (i HiTags) String() string

func (HiTags) StyleName

func (ht HiTags) StyleName() string

StyleName returns the abbreviated 2-3 letter style name of the tag

func (HiTags) SubCategory

func (ht HiTags) SubCategory() HiTags

func (HiTags) ToChroma

func (ht HiTags) ToChroma() chroma.TokenType

ToChroma converts to a chroma.TokenType

func (*HiTags) UnmarshalJSON

func (ev *HiTags) UnmarshalJSON(b []byte) error

func (*HiTags) UnmarshalText

func (ev *HiTags) UnmarshalText(b []byte) error

type Style

type Style map[HiTags]StyleEntry

Style is a full style map of styles for different HiTags tag values

func AvailStyle

func AvailStyle(nm StyleName) Style

AvailStyle returns a style by name from the AvailStyles list -- if not found default is used as a fallback

func (*Style) CopyFrom

func (hs *Style) CopyFrom(ss Style)

CopyFrom copies a style from source style

func (*Style) FromChroma

func (hs *Style) FromChroma(cs *chroma.Style)

FromChroma copies styles from chroma

func (Style) OpenJSON

func (hs Style) OpenJSON(filename gi.FileName) error

Open hi style from a JSON-formatted file.

func (Style) SaveJSON

func (hs Style) SaveJSON(filename gi.FileName) error

Save hi style to a JSON-formatted file.

func (Style) Tag

func (hs Style) Tag(tag HiTags) StyleEntry

Tag returns a StyleEntry for given Tag. Will try sub-category or category if an exact match is not found. does NOT add the background properties -- those are always kept separate.

func (Style) TagRaw

func (hs Style) TagRaw(tag HiTags) StyleEntry

TagRaw returns a StyleEntry for given tag without any inheritance of anything will be IsZero if not defined for this style

func (Style) ToCSS

func (hs Style) ToCSS() map[HiTags]string

ToCSS generates a CSS style sheet for this style, by HiTags tag

func (Style) ToProps

func (hs Style) ToProps() ki.Props

ToProps generates list of ki.Props for this style

type StyleEntry

type StyleEntry struct {
	Color      gi.Color `desc:"text color"`
	Background gi.Color `desc:"background color"`
	Border     gi.Color `view:"-" desc:"border color? not sure what this is -- not really used"`
	Bold       Trilean  `desc:"bold font"`
	Italic     Trilean  `desc:"italic font"`
	Underline  Trilean  `desc:"underline"`
	NoInherit  bool     `desc:"don't inherit these settings from sub-category or category levels -- otherwise everthing with a Pass is inherited"`
}

StyleEntry is one value in the map of highilight style values

func StyleEntryFromChroma

func StyleEntryFromChroma(ce chroma.StyleEntry) StyleEntry

StyleEntryFromChroma returns a new style entry from corresponding chroma version

func (*StyleEntry) FromChroma

func (he *StyleEntry) FromChroma(ce chroma.StyleEntry)

FromChroma copies styles from chroma

func (StyleEntry) Inherit

func (s StyleEntry) Inherit(ancestors ...StyleEntry) StyleEntry

Inherit styles from ancestors.

Ancestors should be provided from oldest, furthest away to newest, closest.

func (StyleEntry) IsZero

func (s StyleEntry) IsZero() bool

func (StyleEntry) String

func (se StyleEntry) String() string

func (StyleEntry) Sub

func (s StyleEntry) Sub(e StyleEntry) StyleEntry

Sub subtracts two style entries, returning an entry with only the differences set

func (StyleEntry) ToCSS

func (se StyleEntry) ToCSS() string

ToCSS converts StyleEntry to CSS attributes.

func (StyleEntry) ToProps

func (se StyleEntry) ToProps() ki.Props

ToProps converts StyleEntry to ki.Props attributes.

type StyleName

type StyleName string

StyleName is a highlighting style name

type Styles

type Styles map[string]Style

Styles is a collection of styles

var AvailStyles Styles

AvailStyles are all highlighting styles

var StdStyles Styles

StdStyles are the styles from chroma package

func (*Styles) Add

func (hs *Styles) Add() Style

Add adds a new style to the list

func (*Styles) CopyFrom

func (hs *Styles) CopyFrom(os Styles)

CopyFrom copies styles from another collection

func (*Styles) FromChroma

func (hs *Styles) FromChroma(cs map[string]*chroma.Style)

FromChroma copies styles from chroma

func (*Styles) Names

func (hs *Styles) Names() []string

Names outputs names of styles in collection

func (*Styles) OpenJSON

func (hs *Styles) OpenJSON(filename gi.FileName) error

Open hi styles from a JSON-formatted file.

func (*Styles) OpenPrefs

func (hs *Styles) OpenPrefs() error

OpenPrefs opens Styles from App standard prefs directory, using PrefsStylesFileName

func (*Styles) SaveJSON

func (hs *Styles) SaveJSON(filename gi.FileName) error

Save hi styles to a JSON-formatted file.

func (*Styles) SavePrefs

func (hs *Styles) SavePrefs() error

SavePrefs saves Styles to App standard prefs directory, using PrefsStylesFileName

func (*Styles) ViewStd

func (hs *Styles) ViewStd()

ViewStd shows the standard styles that are compiled into the program via chroma package

type Trilean

type Trilean uint8

Trilean value for StyleEntry value inheritance.

const (
	Pass Trilean = iota
	Yes
	No

	TrileanN
)

func (*Trilean) FromString

func (i *Trilean) FromString(s string) error

func (Trilean) MarshalJSON

func (ev Trilean) MarshalJSON() ([]byte, error)

func (Trilean) Prefix

func (t Trilean) Prefix(s string) string

func (Trilean) String

func (i Trilean) String() string

func (*Trilean) UnmarshalJSON

func (ev *Trilean) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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