style.go

  1package ansi
  2
  3// Chroma holds all the chroma settings.
  4type Chroma struct {
  5	Text                StylePrimitive `json:"text,omitempty"`
  6	Error               StylePrimitive `json:"error,omitempty"`
  7	Comment             StylePrimitive `json:"comment,omitempty"`
  8	CommentPreproc      StylePrimitive `json:"comment_preproc,omitempty"`
  9	Keyword             StylePrimitive `json:"keyword,omitempty"`
 10	KeywordReserved     StylePrimitive `json:"keyword_reserved,omitempty"`
 11	KeywordNamespace    StylePrimitive `json:"keyword_namespace,omitempty"`
 12	KeywordType         StylePrimitive `json:"keyword_type,omitempty"`
 13	Operator            StylePrimitive `json:"operator,omitempty"`
 14	Punctuation         StylePrimitive `json:"punctuation,omitempty"`
 15	Name                StylePrimitive `json:"name,omitempty"`
 16	NameBuiltin         StylePrimitive `json:"name_builtin,omitempty"`
 17	NameTag             StylePrimitive `json:"name_tag,omitempty"`
 18	NameAttribute       StylePrimitive `json:"name_attribute,omitempty"`
 19	NameClass           StylePrimitive `json:"name_class,omitempty"`
 20	NameConstant        StylePrimitive `json:"name_constant,omitempty"`
 21	NameDecorator       StylePrimitive `json:"name_decorator,omitempty"`
 22	NameException       StylePrimitive `json:"name_exception,omitempty"`
 23	NameFunction        StylePrimitive `json:"name_function,omitempty"`
 24	NameOther           StylePrimitive `json:"name_other,omitempty"`
 25	Literal             StylePrimitive `json:"literal,omitempty"`
 26	LiteralNumber       StylePrimitive `json:"literal_number,omitempty"`
 27	LiteralDate         StylePrimitive `json:"literal_date,omitempty"`
 28	LiteralString       StylePrimitive `json:"literal_string,omitempty"`
 29	LiteralStringEscape StylePrimitive `json:"literal_string_escape,omitempty"`
 30	GenericDeleted      StylePrimitive `json:"generic_deleted,omitempty"`
 31	GenericEmph         StylePrimitive `json:"generic_emph,omitempty"`
 32	GenericInserted     StylePrimitive `json:"generic_inserted,omitempty"`
 33	GenericStrong       StylePrimitive `json:"generic_strong,omitempty"`
 34	GenericSubheading   StylePrimitive `json:"generic_subheading,omitempty"`
 35	Background          StylePrimitive `json:"background,omitempty"`
 36}
 37
 38// StylePrimitive holds all the basic style settings.
 39type StylePrimitive struct {
 40	BlockPrefix     string  `json:"block_prefix,omitempty"`
 41	BlockSuffix     string  `json:"block_suffix,omitempty"`
 42	Prefix          string  `json:"prefix,omitempty"`
 43	Suffix          string  `json:"suffix,omitempty"`
 44	Color           *string `json:"color,omitempty"`
 45	BackgroundColor *string `json:"background_color,omitempty"`
 46	Underline       *bool   `json:"underline,omitempty"`
 47	Bold            *bool   `json:"bold,omitempty"`
 48	Upper           *bool   `json:"upper,omitempty"`
 49	Lower           *bool   `json:"lower,omitempty"`
 50	Title           *bool   `json:"title,omitempty"`
 51	Italic          *bool   `json:"italic,omitempty"`
 52	CrossedOut      *bool   `json:"crossed_out,omitempty"`
 53	Faint           *bool   `json:"faint,omitempty"`
 54	Conceal         *bool   `json:"conceal,omitempty"`
 55	Inverse         *bool   `json:"inverse,omitempty"`
 56	Blink           *bool   `json:"blink,omitempty"`
 57	Format          string  `json:"format,omitempty"`
 58}
 59
 60// StyleTask holds the style settings for a task item.
 61type StyleTask struct {
 62	StylePrimitive
 63	Ticked   string `json:"ticked,omitempty"`
 64	Unticked string `json:"unticked,omitempty"`
 65}
 66
 67// StyleBlock holds the basic style settings for block elements.
 68type StyleBlock struct {
 69	StylePrimitive
 70	Indent      *uint   `json:"indent,omitempty"`
 71	IndentToken *string `json:"indent_token,omitempty"`
 72	Margin      *uint   `json:"margin,omitempty"`
 73}
 74
 75// StyleCodeBlock holds the style settings for a code block.
 76type StyleCodeBlock struct {
 77	StyleBlock
 78	Theme  string  `json:"theme,omitempty"`
 79	Chroma *Chroma `json:"chroma,omitempty"`
 80}
 81
 82// StyleList holds the style settings for a list.
 83type StyleList struct {
 84	StyleBlock
 85	LevelIndent uint `json:"level_indent,omitempty"`
 86}
 87
 88// StyleTable holds the style settings for a table.
 89type StyleTable struct {
 90	StyleBlock
 91	CenterSeparator *string `json:"center_separator,omitempty"`
 92	ColumnSeparator *string `json:"column_separator,omitempty"`
 93	RowSeparator    *string `json:"row_separator,omitempty"`
 94}
 95
 96// StyleConfig is used to configure the styling behavior of an ANSIRenderer.
 97type StyleConfig struct {
 98	Document   StyleBlock `json:"document,omitempty"`
 99	BlockQuote StyleBlock `json:"block_quote,omitempty"`
100	Paragraph  StyleBlock `json:"paragraph,omitempty"`
101	List       StyleList  `json:"list,omitempty"`
102
103	Heading StyleBlock `json:"heading,omitempty"`
104	H1      StyleBlock `json:"h1,omitempty"`
105	H2      StyleBlock `json:"h2,omitempty"`
106	H3      StyleBlock `json:"h3,omitempty"`
107	H4      StyleBlock `json:"h4,omitempty"`
108	H5      StyleBlock `json:"h5,omitempty"`
109	H6      StyleBlock `json:"h6,omitempty"`
110
111	Text           StylePrimitive `json:"text,omitempty"`
112	Strikethrough  StylePrimitive `json:"strikethrough,omitempty"`
113	Emph           StylePrimitive `json:"emph,omitempty"`
114	Strong         StylePrimitive `json:"strong,omitempty"`
115	HorizontalRule StylePrimitive `json:"hr,omitempty"`
116
117	Item        StylePrimitive `json:"item,omitempty"`
118	Enumeration StylePrimitive `json:"enumeration,omitempty"`
119	Task        StyleTask      `json:"task,omitempty"`
120
121	Link     StylePrimitive `json:"link,omitempty"`
122	LinkText StylePrimitive `json:"link_text,omitempty"`
123
124	Image     StylePrimitive `json:"image,omitempty"`
125	ImageText StylePrimitive `json:"image_text,omitempty"`
126
127	Code      StyleBlock     `json:"code,omitempty"`
128	CodeBlock StyleCodeBlock `json:"code_block,omitempty"`
129
130	Table StyleTable `json:"table,omitempty"`
131
132	DefinitionList        StyleBlock     `json:"definition_list,omitempty"`
133	DefinitionTerm        StylePrimitive `json:"definition_term,omitempty"`
134	DefinitionDescription StylePrimitive `json:"definition_description,omitempty"`
135
136	HTMLBlock StyleBlock `json:"html_block,omitempty"`
137	HTMLSpan  StyleBlock `json:"html_span,omitempty"`
138}
139
140func cascadeStyles(s ...StyleBlock) StyleBlock {
141	var r StyleBlock
142	for _, v := range s {
143		r = cascadeStyle(r, v, true)
144	}
145	return r
146}
147
148func cascadeStylePrimitives(s ...StylePrimitive) StylePrimitive {
149	var r StylePrimitive
150	for _, v := range s {
151		r = cascadeStylePrimitive(r, v, true)
152	}
153	return r
154}
155
156func cascadeStylePrimitive(parent, child StylePrimitive, toBlock bool) StylePrimitive {
157	s := child
158
159	s.Color = parent.Color
160	s.BackgroundColor = parent.BackgroundColor
161	s.Underline = parent.Underline
162	s.Bold = parent.Bold
163	s.Upper = parent.Upper
164	s.Title = parent.Title
165	s.Lower = parent.Lower
166	s.Italic = parent.Italic
167	s.CrossedOut = parent.CrossedOut
168	s.Faint = parent.Faint
169	s.Conceal = parent.Conceal
170	s.Inverse = parent.Inverse
171	s.Blink = parent.Blink
172
173	if toBlock {
174		s.BlockPrefix = parent.BlockPrefix
175		s.BlockSuffix = parent.BlockSuffix
176		s.Prefix = parent.Prefix
177		s.Suffix = parent.Suffix
178	}
179
180	if child.Color != nil {
181		s.Color = child.Color
182	}
183	if child.BackgroundColor != nil {
184		s.BackgroundColor = child.BackgroundColor
185	}
186	if child.Underline != nil {
187		s.Underline = child.Underline
188	}
189	if child.Bold != nil {
190		s.Bold = child.Bold
191	}
192	if child.Upper != nil {
193		s.Upper = child.Upper
194	}
195	if child.Lower != nil {
196		s.Lower = child.Lower
197	}
198	if child.Title != nil {
199		s.Title = child.Title
200	}
201	if child.Italic != nil {
202		s.Italic = child.Italic
203	}
204	if child.CrossedOut != nil {
205		s.CrossedOut = child.CrossedOut
206	}
207	if child.Faint != nil {
208		s.Faint = child.Faint
209	}
210	if child.Conceal != nil {
211		s.Conceal = child.Conceal
212	}
213	if child.Inverse != nil {
214		s.Inverse = child.Inverse
215	}
216	if child.Blink != nil {
217		s.Blink = child.Blink
218	}
219	if child.BlockPrefix != "" {
220		s.BlockPrefix = child.BlockPrefix
221	}
222	if child.BlockSuffix != "" {
223		s.BlockSuffix = child.BlockSuffix
224	}
225	if child.Prefix != "" {
226		s.Prefix = child.Prefix
227	}
228	if child.Suffix != "" {
229		s.Suffix = child.Suffix
230	}
231	if child.Format != "" {
232		s.Format = child.Format
233	}
234
235	return s
236}
237
238func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock {
239	s := child
240	s.StylePrimitive = cascadeStylePrimitive(parent.StylePrimitive, child.StylePrimitive, toBlock)
241
242	if toBlock {
243		s.Indent = parent.Indent
244		s.Margin = parent.Margin
245	}
246
247	if child.Indent != nil {
248		s.Indent = child.Indent
249	}
250
251	return s
252}