git.greeks.studio/lethews/hugo@v0.47.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 (
    26  	_ cmder = (*genChromaStyles)(nil)
    27  )
    28  
    29  type genChromaStyles struct {
    30  	style          string
    31  	highlightStyle string
    32  	linesStyle     string
    33  	*baseCmd
    34  }
    35  
    36  // TODO(bep) highlight
    37  func createGenChromaStyles() *genChromaStyles {
    38  	g := &genChromaStyles{
    39  		baseCmd: newBaseCmd(&cobra.Command{
    40  			Use:   "chromastyles",
    41  			Short: "Generate CSS stylesheet for the Chroma code highlighter",
    42  			Long: `Generate CSS stylesheet for the Chroma code highlighter for a given style. This stylesheet is needed if pygmentsUseClasses is enabled in config.
    43  
    44  See https://help.farbox.com/pygments.html for preview of available styles`,
    45  		}),
    46  	}
    47  
    48  	g.cmd.RunE = func(cmd *cobra.Command, args []string) error {
    49  		return g.generate()
    50  	}
    51  
    52  	g.cmd.PersistentFlags().StringVar(&g.style, "style", "friendly", "highlighter style (see https://help.farbox.com/pygments.html)")
    53  	g.cmd.PersistentFlags().StringVar(&g.highlightStyle, "highlightStyle", "bg:#ffffcc", "style used for highlighting lines (see https://github.com/alecthomas/chroma)")
    54  	g.cmd.PersistentFlags().StringVar(&g.linesStyle, "linesStyle", "", "style used for line numbers (see https://github.com/alecthomas/chroma)")
    55  
    56  	return g
    57  }
    58  
    59  func (g *genChromaStyles) generate() error {
    60  	builder := styles.Get(g.style).Builder()
    61  	if g.highlightStyle != "" {
    62  		builder.Add(chroma.LineHighlight, g.highlightStyle)
    63  	}
    64  	if g.linesStyle != "" {
    65  		builder.Add(chroma.LineNumbers, g.linesStyle)
    66  	}
    67  	style, err := builder.Build()
    68  	if err != nil {
    69  		return err
    70  	}
    71  	formatter := html.New(html.WithClasses())
    72  	formatter.WriteCSS(os.Stdout, style)
    73  	return nil
    74  }