Skip to content

Commit

Permalink
Merge branch 'tocas4'
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Mar 3, 2023
2 parents adc10ef + 22a2919 commit f828912
Show file tree
Hide file tree
Showing 1,403 changed files with 66,309 additions and 140,983 deletions.
4 changes: 2 additions & 2 deletions README-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
只需要將下列 HTML 標籤複製至你的 `<head>...</head>` 網頁段落就可以開工了 🔥。

```html
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.1.0/tocas.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.1.0/tocas.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.2.0/tocas.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.2.0/tocas.min.js"></script>
```

又或者你喜歡 `npm` 的話…👀
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ The fastest and most intuitive way to build diverse websites and progressive web
Just copy the HTML tags to your `<head>...</head>` section and have a blast🔥.

```html
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.1.0/tocas.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.1.0/tocas.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.2.0/tocas.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocas/4.2.0/tocas.min.js"></script>
```

Or maybe you like `npm` 👀
Expand Down
7 changes: 7 additions & 0 deletions cli/docs-builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Docs Builder

```bash
$ npm i minify -g
$ npm i js-beautify -g
$ npm i highlight.js-cli -g
```
36 changes: 27 additions & 9 deletions cli/docs-builder/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ type Article struct {
Description string `yaml:"Description"`
Type string `yaml:"Type"`
Intro string `yaml:"Intro"`
Links []ArticleLink `yaml:"Links"`
Example ArticleExample `yaml:"Example"`
Examples []ArticleExampleSection `yaml:"Examples"`
Variables []ArticleVariable `yaml:"Variables"`
Remove []string `yaml:"Remove"`
Relatives []string `yaml:"Relatives"`
Definitions []ArticleDefinition `yaml:"Definitions"`
Expand All @@ -29,6 +31,21 @@ type ArticleExample struct {
FormattedHTML template.HTML
}

// ArticleLink
type ArticleLink struct {
Icon string `yaml:"Icon"`
Title string `yaml:"Title"`
Description string `yaml:"Description"`
Link string `yaml:"Link"`
}

// ArticleVariable
type ArticleVariable struct {
Name string `yaml:"Name"`
Example string `yaml:"Example"`
Description string `yaml:"Description"`
}

// ArticleDefinition
type ArticleDefinition struct {
Title string `yaml:"Title"`
Expand All @@ -40,15 +57,16 @@ type ArticleDefinition struct {

// ArticleDefinitionSection
type ArticleDefinitionSection struct {
Title string `yaml:"Title"`
Description string `yaml:"Description"`
Version string `yaml:"Version"`
HTML string `yaml:"HTML"`
Responsive bool `yaml:"Responsive"`
Since string `yaml:"Since"`
Icon string `yaml:"Icon"`
Remove []string `yaml:"Remove"`
Anchor string `yaml:"Anchor"`
Title string `yaml:"Title"`
Description string `yaml:"Description"`
Version string `yaml:"Version"`
HTML string `yaml:"HTML"`
Responsive bool `yaml:"Responsive"`
Since string `yaml:"Since"`
Icon string `yaml:"Icon"`
Remove []string `yaml:"Remove"`
Anchor string `yaml:"Anchor"`
IsDownloadable bool `yaml:"IsDownloadable"`
//
AttachedHTML string `yaml:"AttachedHTML"`
//
Expand Down
Binary file modified cli/docs-builder/docs-builder
Binary file not shown.
Binary file added cli/docs-builder/docs-builder.exe
Binary file not shown.
31 changes: 31 additions & 0 deletions cli/docs-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ func main() {
Aliases: []string{"l"},
Usage: "指定要載入的相關語系代號,只能同時間指定一個,例如:`zh-tw`。",
},
&cli.StringFlag{
Name: "screenshot-path",
Usage: "會讓 Chrome Headless 螢幕截圖的本地 file:/// 路徑,例如:`file:///C:/Users/yami/tocas/examples/`",
},
&cli.StringFlag{
Name: "mailto",
Usage: "Gmail 的電子郵件地址。",
},
&cli.StringFlag{
Name: "mailpwd",
Usage: "Gmail 的應用程式密碼。",
},
&cli.StringFlag{
Name: "mailtmpl",
Usage: "要寄送信件的 HTML 檔案路徑。",
},
},
Commands: []*cli.Command{
{
Expand All @@ -29,6 +45,21 @@ func main() {
Usage: "會將 Tocas UI 的 /src 轉譯至 /dist。",
Action: pack,
},
{
Name: "screenshot",
Usage: "會透過 Chrome Headless 螢幕截圖資料夾裡的所有網頁並且存到 /examples/screenshots。",
Action: screenshot,
},
{
Name: "compress",
Usage: "",
Action: compressScreenshots,
},
{
Name: "mail",
Usage: "會透過 Gmail 寄送指定網頁給指定的收件人,用來測試 Email 模板。",
Action: testMail,
},
},
}
err := app.Run(os.Args)
Expand Down
60 changes: 58 additions & 2 deletions cli/docs-builder/main_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"
"time"

"github.com/russross/blackfriday/v2"
cli "github.com/urfave/cli/v2"
Expand All @@ -35,20 +37,35 @@ func build(c *cli.Context) error {
return err
}
// 重新複製一份模板的 `/assets` 進去。
log.Printf("正在複製模板 /assets")
if err := exec.Command("rm", "-rf", "./../../docs/"+c.String("lang")+"/assets").Run(); err != nil {
return err
}
if err := exec.Command("cp", "-rf", "./templates/assets", "./../../docs/"+c.String("lang")+"/assets").Run(); err != nil {
return err
}
// 重新複製一份 `/src` 到文件的 `/assets/tocas` 進去。
log.Printf("正在打包成為 /dist")
if err := pack(c); err != nil {
return err
}
log.Printf("正在複製 /dist 到本語系的 /assets/tocas")
if err := exec.Command("rm", "-rf", "./../../docs/"+c.String("lang")+"/assets/tocas").Run(); err != nil {
return err
}
if err := exec.Command("cp", "-rf", "./../../src", "./../../docs/"+c.String("lang")+"/assets/tocas").Run(); err != nil {
if err := exec.Command("cp", "-rf", "./../../dist", "./../../docs/"+c.String("lang")+"/assets/tocas").Run(); err != nil {
return err
}

// 重新複製一份 `/src` 到文件的 `/assets/tocas` 進去。
if err := exec.Command("rm", "-rf", "./../../docs/"+c.String("lang")+"/assets/tocas/src").Run(); err != nil {
return err
}
if err := exec.Command("cp", "-rf", "./../../src", "./../../docs/"+c.String("lang")+"/assets/tocas/src").Run(); err != nil {
return err
}

// 重新複製一份 `/examples` 到文件的 `/examples` 進去。
log.Printf("正在複製 /examples")
if err := exec.Command("rm", "-rf", "./../../docs/"+c.String("lang")+"/examples").Run(); err != nil {
return err
}
Expand Down Expand Up @@ -168,6 +185,14 @@ func build(c *cli.Context) error {
for ji, j := range v.Sections {
// 如果這個段落有附加的 HTML 片段,就透過 Highlight JS 跟 Beautify 處理。
if j.AttachedHTML != "" {
if strings.Contains(j.AttachedHTML, "{- email.css -}") {
b, err := os.ReadFile("./../../src/email.css")
if err != nil {
panic(err)
}
j.AttachedHTML = cssminify(string(b))
}

article.Definitions[vi].Sections[ji].FormattedHTML = tmplCode(trim(trim(j.AttachedHTML, j.Remove), article.Remove))
}
// 如果這個段落有 HTML 標籤內容,就透過 Highlight JS 跟 Beautify 處理。
Expand Down Expand Up @@ -250,6 +275,37 @@ func highlight(s string) string {
return string(output)
}

// cssminify 會將透過 css-minify 最小化 CSS 樣式。
func cssminify(content string) string {
hash := fmt.Sprintf("%x", md5.Sum([]byte(content)))
b, err := ioutil.ReadFile("./caches/cssminify/" + hash)
if err != nil {
os.MkdirAll("./caches/cssminify/", 0777)
}
if len(b) != 0 {
return string(b)
}
tmp, err := os.Create("./caches/cssminify/" + strconv.Itoa(int(time.Now().Unix())))
if err != nil {
panic(err)
}
if _, err := tmp.WriteString(content); err != nil {
panic(err)
}
if err := tmp.Close(); err != nil {
panic(err)
}
if err := exec.Command("css-minify", "-f", tmp.Name(), "-o", "./caches/cssminify/").Run(); err != nil {
panic(err)
}
b, err = os.ReadFile(tmp.Name() + ".min.css")
if err != nil {
panic(err)
}
ioutil.WriteFile("./caches/cssminify/"+hash, b, 0777)
return string(b)
}

// beautify 會透過 js-beautify 美化程式碼。
func beautify(s string, typ string) string {
hash := fmt.Sprintf("%x", md5.Sum([]byte(s)))
Expand Down
52 changes: 52 additions & 0 deletions cli/docs-builder/main_mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"net/smtp"
"os"
"os/exec"
"strings"

cli "github.com/urfave/cli/v2"
)

func testMail(c *cli.Context) error {
from := c.String("mailto")
pass := c.String("mailpwd")
to := c.String("mailto")

// 透過 `css-minify` 將 `/dist/tocas.css` 的內容最小化。
if err := exec.Command("css-minify", "-f", "./../../src/email.css", "-o", "./").Run(); err != nil {
return err
}

b, err := os.ReadFile("./email.min.css")
if err != nil {
return err
}

bb, err := os.ReadFile(c.String("mailtmpl"))
if err != nil {
return err
}

body := string(bb)

body = strings.ReplaceAll(body, `<link rel="stylesheet" href="./email.css" />`, fmt.Sprintf(`<style type="text/css">%s</style>`, string(b)))

msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: 測試郵件\n" +
"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" +
body

err = smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
from, []string{to}, []byte(msg))

if err != nil {
return err
}

return nil
}
35 changes: 34 additions & 1 deletion cli/docs-builder/main_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func pack(c *cli.Context) error {
log.Fatal(err)
}
tocas := string(b)

var content string
// 找出所有被 `@import` 的檔案並載入其內容,然後把 `tocas.css` 裡的 `@import` 換成真實的內容。
for _, v := range regexp.MustCompile(`@import ".\/(.*?)";`).FindAllStringSubmatch(string(b), -1) {
Expand All @@ -30,6 +29,10 @@ func pack(c *cli.Context) error {
tocas = strings.ReplaceAll(tocas, v[0], "")
content += string(b) + "\n"
}
//
if err := os.MkdirAll("./../../dist", 0777); err != nil {
log.Fatal(err)
}
// 將這個新的組合原始碼儲存至 `/dist/tocas.css`。
err = os.WriteFile("./../../dist/tocas.css", []byte(content+tocas), 0777)
if err != nil {
Expand All @@ -39,6 +42,36 @@ func pack(c *cli.Context) error {
if err := exec.Command("css-minify", "-f", "./../../dist/tocas.css", "-o", "./../../dist/").Run(); err != nil {
log.Fatal(err)
}
// 先載入 `/src/scripts/tocas.js` 的內容。
b, err = os.ReadFile("./../../src/scripts/tocas.js")
if err != nil {
log.Fatal(err)
}
tocasjs := string(b)

content = ""
// 找出所有被 `@import` 的檔案並載入其內容,然後把 `tocas.css` 裡的 `@import` 換成真實的內容。
for _, v := range regexp.MustCompile(`// @import "(.*?)";`).FindAllStringSubmatch(string(b), -1) {
b, err := os.ReadFile("./../../src/scripts/" + v[1])
if err != nil {
log.Fatal(err)
}
// 讀取之後就把這行 `@import` 從原本的 `tocas.js` 裡取代掉。
tocasjs = strings.ReplaceAll(tocasjs, v[0], string(b)+"\n")
}
// 將這個新的組合原始碼儲存至 `/dist/tocas.js`。
err = os.WriteFile("./../../dist/tocas.js", []byte(content+tocasjs), 0777)
if err != nil {
log.Fatal(err)
}
// 使用 `minify` 將 `/dist/tocas.js` 縮小一份並輸出到 `/dist/tocas.min.js`
b, err = exec.Command("minify", "./../../dist/tocas.js").Output()
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile("./../../dist/tocas.min.js", b, 0644); err != nil {
log.Fatal(err)
}
// 移除舊有的 `/dist/fonts` 並從 `/src/fonts` 複製過去一份。
if err := exec.Command("rm", "-rf", "./../../dist/fonts").Run(); err != nil {
log.Fatal(err)
Expand Down
18 changes: 18 additions & 0 deletions cli/docs-builder/main_png.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"log"
"os/exec"

cli "github.com/urfave/cli/v2"
)

func compressScreenshots(c *cli.Context) error {
// pngquant -f --ext .png ./../../examples/screenshots/*.png
b, err := exec.Command("pngquant", "-f", "--ext", ".png", "./../../examples/screenshots/*.png").CombinedOutput()
if err != nil {
log.Println(string(b))
return err
}
return nil
}
Loading

0 comments on commit f828912

Please sign in to comment.