aboutsummaryrefslogtreecommitdiff
path: root/devtools/cmd/css
diff options
context:
space:
mode:
authorJulie Qiu <julie@golang.org>2020-09-09 00:21:53 -0400
committerJulie Qiu <julie@golang.org>2020-09-09 21:13:57 +0000
commita5c6be2ad47bc8eedd1286aaebb78a1c3e23709c (patch)
treeaf06da700ef96f6f43b8e6524a64451f48533e55 /devtools/cmd/css
parent909213404b236e91c97940babcfcc37af06a13ae (diff)
downloadgo-x-pkgsite-a5c6be2ad47bc8eedd1286aaebb78a1c3e23709c.tar.xz
devtools/cmd/css: moved from content/static/css
content/static/css is moved to devtools/cmd/css (pure code in motion). Change-Id: I7b48bf017edffb1b4c3c571ed44bac7e09e37918 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/253608 Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'devtools/cmd/css')
-rw-r--r--devtools/cmd/css/main.go142
1 files changed, 142 insertions, 0 deletions
diff --git a/devtools/cmd/css/main.go b/devtools/cmd/css/main.go
new file mode 100644
index 00000000..d6246d7c
--- /dev/null
+++ b/devtools/cmd/css/main.go
@@ -0,0 +1,142 @@
+// 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 main 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 (
+ "bufio"
+ "flag"
+ "fmt"
+ "log"
+ "net/http"
+ "os"
+ "path"
+ "runtime"
+ "strings"
+)
+
+const (
+ githubStylesheet = "https://raw.githubusercontent.com/sindresorhus/github-markdown-css/gh-pages/github-markdown.css"
+ githubREADMEClass = ".markdown-body"
+ discoveryREADMEClass = ".Overview-readmeContent"
+)
+
+var write = flag.Bool("write", false, "append modifications to content/static/css/stylesheet.css")
+
+func main() {
+ flag.Parse()
+
+ resp, err := http.Get(githubStylesheet)
+ if err != nil {
+ log.Fatalf("http.Get(%q): %v", githubStylesheet, err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ log.Fatalf("http.Get(%q): status = %d", githubStylesheet, resp.StatusCode)
+ }
+
+ defer resp.Body.Close()
+ scanner := bufio.NewScanner(resp.Body)
+ var (
+ atPropertyStart = true
+ curr string
+ includeProperty bool
+ properties []string
+ )
+ for scanner.Scan() {
+ text := scanner.Text()
+ if atPropertyStart && shouldIncludeProperty(text) {
+ includeProperty = true
+ }
+ if text == "}" {
+ if includeProperty {
+ properties = append(properties, curr+text+"\n")
+ }
+ curr = ""
+ includeProperty = false
+ atPropertyStart = true
+ continue
+ }
+ if includeProperty {
+ curr += text
+ curr += "\n"
+ }
+ }
+
+ if err := scanner.Err(); err != nil {
+ log.Fatal(err)
+ }
+
+ // Get the abs path for content/static/css.
+ _, filename, _, ok := runtime.Caller(0)
+ if !ok {
+ log.Fatal("No caller information")
+ }
+ dir := path.Dir(filename)
+
+ f := dir + "/stylesheet.css"
+ file, err := os.OpenFile(f, os.O_WRONLY|os.O_APPEND, 0644)
+ if err != nil {
+ log.Fatalf("os.OpenFile(f, os.O_WRONLY|os.O_APPEND, 0644): %v", err)
+ }
+ defer func() {
+ if err := file.Close(); err != nil {
+ log.Fatalf("file.Close(): %v", err)
+ }
+ }()
+
+ if !*write {
+ fmt.Println("Dryrun only. Run with `-write` to write to stylesheet.css.")
+ } else {
+ fmt.Printf("Writing these properties to %q: \n", f)
+ }
+
+ contentsToWrite := `
+/* ---------- */
+/*
+/* The CSS classes below are generated using content/static/css/main.go
+/* To update, delete the contents below and and run go run content/static/css/main.go
+/*
+/* ---------- */`
+ contentsToWrite += "\n\n"
+
+ for _, p := range properties {
+ contentsToWrite += strings.ReplaceAll(p, githubREADMEClass, discoveryREADMEClass)
+ }
+
+ contentsToWrite += `
+/* ---------- */
+/*
+/* End output from content/static/css/main.go.
+/*
+/* ---------- */`
+
+ fmt.Println(contentsToWrite)
+
+ if _, err := file.WriteString(contentsToWrite); err != nil {
+ log.Fatalf("file.WriteString(%q): %v", contentsToWrite, err)
+ }
+}
+
+// shouldIncludeProperty reports whether this property should be included in
+// the CSS file.
+func shouldIncludeProperty(property string) bool {
+ parts := strings.Split(property, " ")
+ if len(parts) < 1 {
+ return false
+ }
+ if parts[0] != githubREADMEClass {
+ return false
+ }
+ for _, p := range parts[1:] {
+ if strings.HasPrefix(p, ".") {
+ return false
+ }
+ }
+ return true
+}