Copyright 2019 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Command css appends CSS styles to content/static/stylesheet.css. It reads from the CSS file at https://github.com/sindresorhus/github-markdown-css/blob/gh-pages/github-markdown.css and removes all styles that do not belong to a .markdown-body <tag>. The .markdown-body class is then replaced with .Overview-readmeContent, for use in the discovery codebase. The remaining properties are written to content/static/css/stylesheet.css.
package main

import (
	
	
	
	
	
	
	
	
	
	
)

const (
	cssFile              = "content/static/css/readme.css"
	githubStylesheet     = "https://raw.githubusercontent.com/sindresorhus/github-markdown-css/gh-pages/github-markdown.css"
	githubREADMEClass    = ".markdown-body"
	discoveryREADMEClass = ".Overview-readmeContent"
	copyright            = `/*
* Copyright 2019-2020 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
`
)

var write = flag.Bool("write", false, "append modifications to content/static/css/stylesheet.css")

func () {
	flag.Parse()

	,  := http.Get(githubStylesheet)
	if  != nil {
		log.Fatalf("http.Get(%q): %v", githubStylesheet, )
	}
	if .StatusCode != http.StatusOK {
		log.Fatalf("http.Get(%q): status = %d", githubStylesheet, .StatusCode)
	}

	defer .Body.Close()
	 := bufio.NewScanner(.Body)
	var (
		 = true
		            string
		 bool
		      []string
	)
	for .Scan() {
		 := .Text()
		if  := replaceHeaderTag();  != "" {
			 = 
		}
		if  && shouldIncludeProperty() {
			 = true
		}
		if  := replaceValueWithRems();  != "" {
			 = 
		}
		if  == "}" {
			if  {
				 = append(, ++"\n")
			}
			 = ""
			 = false
			 = true
			continue
		}
		if  {
			 += 
			 += "\n"
		}
	}

	if  := .Err();  != nil {
		log.Fatal()
	}

	if  := ioutil.WriteFile(cssFile, []byte(copyright), 0644);  != nil {
		log.Fatalf("ioutil.WriteFile(f, '', 0644): %v", )
	}

	,  := os.OpenFile(cssFile, os.O_WRONLY|os.O_APPEND, 0644)
	if  != nil {
		log.Fatalf("os.OpenFile(f, os.O_WRONLY|os.O_APPEND, 0644): %v", )
	}
	defer func() {
		if  := .Close();  != nil {
			log.Fatalf("file.Close(): %v", )
		}
	}()

	if !*write {
		fmt.Println("Dryrun only. Run with `-write` to write to stylesheet.css.")
	} else {
		fmt.Printf("Writing these properties to %q: \n", cssFile)
	}

	 := `
/* ---------- */
/*
/* The CSS classes below are generated using devtools/cmd/css/main.go
/* If the generated CSS already exists, the file is overwritten
/*
/* ---------- */`
	 += "\n\n"

	for ,  := range  {
		 += strings.ReplaceAll(, githubREADMEClass, discoveryREADMEClass)
	}

	 += `
/* ---------- */
/*
/* End output from devtools/cmd/css/main.go
/*
/* ---------- */`

	fmt.Println()

	if ,  := .WriteString();  != nil {
		log.Fatalf("file.WriteString(%q): %v", , )
	}
}
replaceHeaderTag finds any header tags in a line of text and increases the header level by 2. replaceHeader tag returns the replaced string if a header tag is found and returns an empty string if not
func ( string) string {
	 := map[string]string{
		"h1": "h3", "h2": "h4", "h3": "h5", "h4": "h6", "h5": "div[aria-level=7]", "h6": "div[aria-level=8]",
	}
	for ,  := range  {
		if strings.Contains(, ) {
			return strings.ReplaceAll(, , )
		}
	}
	return ""
}
shouldIncludeProperty reports whether this property should be included in the CSS file.
func ( string) bool {
	 := strings.Split(, " ")
	if len() < 1 {
		return false
	}
	if [0] != githubREADMEClass {
		return false
	}
	for ,  := range [1:] {
		if strings.HasPrefix(, ".") {
			return false
		}
	}
	return true
}
pxToRem returns the number value of a px string to a rem string.
func ( string) string {
	,  := strconv.ParseFloat(, 32)
	if  != nil {
		return ""
	}
	 =  / 16
	return fmt.Sprintf("%frem", )
}
replaceValueWithRems replaces the px values in a line of css with rems. e.g: padding: 25px 10px => padding:
func ( string) string {
	var  string
	 := regexp.MustCompile(`([-+]?[0-9]*\.?[0-9]+)px`)
	 := .FindAllStringSubmatchIndex(, -1)
e.g: "padding: 6px 13px;" => "padding: 0.375rem 0.8125rem;" padding: [valueStartIdx][numStartIdx]25[numEndIdx]px[valueEndIdx] 10em; The value here is the full string "25px" and num is just "25".
		, , ,  := [0], [1], [2], [3]
		if  == 0 {
			 += [0:]
		}
		 += pxToRem([:])
		if  == len()-1 {
			 += [:]
If there are more matches for "px", add up until the start of the next match.
			 += [:[+1][0]]
		}
	}
	return