github.com/charmbracelet/glamour@v0.7.0/ansi/style.go (about)

     1  package ansi
     2  
     3  // Chroma holds all the chroma settings.
     4  type 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.
    39  type 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  	Overlined       *bool   `json:"overlined,omitempty"`
    56  	Inverse         *bool   `json:"inverse,omitempty"`
    57  	Blink           *bool   `json:"blink,omitempty"`
    58  	Format          string  `json:"format,omitempty"`
    59  }
    60  
    61  // StyleTask holds the style settings for a task item.
    62  type StyleTask struct {
    63  	StylePrimitive
    64  	Ticked   string `json:"ticked,omitempty"`
    65  	Unticked string `json:"unticked,omitempty"`
    66  }
    67  
    68  // StyleBlock holds the basic style settings for block elements.
    69  type StyleBlock struct {
    70  	StylePrimitive
    71  	Indent      *uint   `json:"indent,omitempty"`
    72  	IndentToken *string `json:"indent_token,omitempty"`
    73  	Margin      *uint   `json:"margin,omitempty"`
    74  }
    75  
    76  // StyleCodeBlock holds the style settings for a code block.
    77  type StyleCodeBlock struct {
    78  	StyleBlock
    79  	Theme  string  `json:"theme,omitempty"`
    80  	Chroma *Chroma `json:"chroma,omitempty"`
    81  }
    82  
    83  // StyleList holds the style settings for a list.
    84  type StyleList struct {
    85  	StyleBlock
    86  	LevelIndent uint `json:"level_indent,omitempty"`
    87  }
    88  
    89  // StyleTable holds the style settings for a table.
    90  type StyleTable struct {
    91  	StyleBlock
    92  	CenterSeparator *string `json:"center_separator,omitempty"`
    93  	ColumnSeparator *string `json:"column_separator,omitempty"`
    94  	RowSeparator    *string `json:"row_separator,omitempty"`
    95  }
    96  
    97  // StyleConfig is used to configure the styling behavior of an ANSIRenderer.
    98  type StyleConfig struct {
    99  	Document   StyleBlock `json:"document,omitempty"`
   100  	BlockQuote StyleBlock `json:"block_quote,omitempty"`
   101  	Paragraph  StyleBlock `json:"paragraph,omitempty"`
   102  	List       StyleList  `json:"list,omitempty"`
   103  
   104  	Heading StyleBlock `json:"heading,omitempty"`
   105  	H1      StyleBlock `json:"h1,omitempty"`
   106  	H2      StyleBlock `json:"h2,omitempty"`
   107  	H3      StyleBlock `json:"h3,omitempty"`
   108  	H4      StyleBlock `json:"h4,omitempty"`
   109  	H5      StyleBlock `json:"h5,omitempty"`
   110  	H6      StyleBlock `json:"h6,omitempty"`
   111  
   112  	Text           StylePrimitive `json:"text,omitempty"`
   113  	Strikethrough  StylePrimitive `json:"strikethrough,omitempty"`
   114  	Emph           StylePrimitive `json:"emph,omitempty"`
   115  	Strong         StylePrimitive `json:"strong,omitempty"`
   116  	HorizontalRule StylePrimitive `json:"hr,omitempty"`
   117  
   118  	Item        StylePrimitive `json:"item,omitempty"`
   119  	Enumeration StylePrimitive `json:"enumeration,omitempty"`
   120  	Task        StyleTask      `json:"task,omitempty"`
   121  
   122  	Link     StylePrimitive `json:"link,omitempty"`
   123  	LinkText StylePrimitive `json:"link_text,omitempty"`
   124  
   125  	Image     StylePrimitive `json:"image,omitempty"`
   126  	ImageText StylePrimitive `json:"image_text,omitempty"`
   127  
   128  	Code      StyleBlock     `json:"code,omitempty"`
   129  	CodeBlock StyleCodeBlock `json:"code_block,omitempty"`
   130  
   131  	Table StyleTable `json:"table,omitempty"`
   132  
   133  	DefinitionList        StyleBlock     `json:"definition_list,omitempty"`
   134  	DefinitionTerm        StylePrimitive `json:"definition_term,omitempty"`
   135  	DefinitionDescription StylePrimitive `json:"definition_description,omitempty"`
   136  
   137  	HTMLBlock StyleBlock `json:"html_block,omitempty"`
   138  	HTMLSpan  StyleBlock `json:"html_span,omitempty"`
   139  }
   140  
   141  func cascadeStyles(s ...StyleBlock) StyleBlock {
   142  	var r StyleBlock
   143  
   144  	for _, v := range s {
   145  		r = cascadeStyle(r, v, true)
   146  	}
   147  	return r
   148  }
   149  
   150  func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock {
   151  	s := child
   152  
   153  	s.Color = parent.Color
   154  	s.BackgroundColor = parent.BackgroundColor
   155  	s.Underline = parent.Underline
   156  	s.Bold = parent.Bold
   157  	s.Upper = parent.Upper
   158  	s.Title = parent.Title
   159  	s.Lower = parent.Lower
   160  	s.Italic = parent.Italic
   161  	s.CrossedOut = parent.CrossedOut
   162  	s.Faint = parent.Faint
   163  	s.Conceal = parent.Conceal
   164  	s.Overlined = parent.Overlined
   165  	s.Inverse = parent.Inverse
   166  	s.Blink = parent.Blink
   167  
   168  	if toBlock {
   169  		s.Indent = parent.Indent
   170  		s.Margin = parent.Margin
   171  		s.BlockPrefix = parent.BlockPrefix
   172  		s.BlockSuffix = parent.BlockSuffix
   173  		s.Prefix = parent.Prefix
   174  		s.Suffix = parent.Suffix
   175  	}
   176  
   177  	if child.Color != nil {
   178  		s.Color = child.Color
   179  	}
   180  	if child.BackgroundColor != nil {
   181  		s.BackgroundColor = child.BackgroundColor
   182  	}
   183  	if child.Indent != nil {
   184  		s.Indent = child.Indent
   185  	}
   186  	if child.Margin != nil {
   187  		s.Margin = child.Margin
   188  	}
   189  	if child.Underline != nil {
   190  		s.Underline = child.Underline
   191  	}
   192  	if child.Bold != nil {
   193  		s.Bold = child.Bold
   194  	}
   195  	if child.Upper != nil {
   196  		s.Upper = child.Upper
   197  	}
   198  	if child.Lower != nil {
   199  		s.Lower = child.Lower
   200  	}
   201  	if child.Title != nil {
   202  		s.Title = child.Title
   203  	}
   204  	if child.Italic != nil {
   205  		s.Italic = child.Italic
   206  	}
   207  	if child.CrossedOut != nil {
   208  		s.CrossedOut = child.CrossedOut
   209  	}
   210  	if child.Faint != nil {
   211  		s.Faint = child.Faint
   212  	}
   213  	if child.Conceal != nil {
   214  		s.Conceal = child.Conceal
   215  	}
   216  	if child.Overlined != nil {
   217  		s.Overlined = child.Overlined
   218  	}
   219  	if child.Inverse != nil {
   220  		s.Inverse = child.Inverse
   221  	}
   222  	if child.Blink != nil {
   223  		s.Blink = child.Blink
   224  	}
   225  	if child.BlockPrefix != "" {
   226  		s.BlockPrefix = child.BlockPrefix
   227  	}
   228  	if child.BlockSuffix != "" {
   229  		s.BlockSuffix = child.BlockSuffix
   230  	}
   231  	if child.Prefix != "" {
   232  		s.Prefix = child.Prefix
   233  	}
   234  	if child.Suffix != "" {
   235  		s.Suffix = child.Suffix
   236  	}
   237  	if child.Format != "" {
   238  		s.Format = child.Format
   239  	}
   240  
   241  	return s
   242  }