github.com/gohugoio/hugo@v0.88.1/commands/genchromastyles.go (about)

     1  // Copyright 2017-present The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package commands
    15  
    16  import (
    17  	"os"
    18  
    19  	"github.com/alecthomas/chroma"
    20  	"github.com/alecthomas/chroma/formatters/html"
    21  	"github.com/alecthomas/chroma/styles"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  var _ cmder = (*genChromaStyles)(nil)
    26  
    27  type genChromaStyles struct {
    28  	style          string
    29  	highlightStyle string
    30  	linesStyle     string
    31  	*baseCmd
    32  }
    33  
    34  // TODO(bep) highlight
    35  func createGenChromaStyles() *genChromaStyles {
    36  	g := &genChromaStyles{
    37  		baseCmd: newBaseCmd(&cobra.Command{
    38  			Use:   "chromastyles",
    39  			Short: "Generate CSS stylesheet for the Chroma code highlighter",
    40  			Long: `Generate CSS stylesheet for the Chroma code highlighter for a given style. This stylesheet is needed if markup.highlight.noClasses is disabled in config.
    41  
    42  See https://xyproto.github.io/splash/docs/all.html for a preview of the available styles`,
    43  		}),
    44  	}
    45  
    46  	g.cmd.RunE = func(cmd *cobra.Command, args []string) error {
    47  		return g.generate()
    48  	}
    49  
    50  	g.cmd.PersistentFlags().StringVar(&g.style, "style", "friendly", "highlighter style (see https://xyproto.github.io/splash/docs/)")
    51  	g.cmd.PersistentFlags().StringVar(&g.highlightStyle, "highlightStyle", "bg:#ffffcc", "style used for highlighting lines (see https://github.com/alecthomas/chroma)")
    52  	g.cmd.PersistentFlags().StringVar(&g.linesStyle, "linesStyle", "", "style used for line numbers (see https://github.com/alecthomas/chroma)")
    53  
    54  	return g
    55  }
    56  
    57  func (g *genChromaStyles) generate() error {
    58  	builder := styles.Get(g.style).Builder()
    59  	if g.highlightStyle != "" {
    60  		builder.Add(chroma.LineHighlight, g.highlightStyle)
    61  	}
    62  	if g.linesStyle != "" {
    63  		builder.Add(chroma.LineNumbers, g.linesStyle)
    64  	}
    65  	style, err := builder.Build()
    66  	if err != nil {
    67  		return err
    68  	}
    69  	formatter := html.New(html.WithAllClasses(true))
    70  	formatter.WriteCSS(os.Stdout, style)
    71  	return nil
    72  }