theme.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package model
  17. import (
  18. "bytes"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "github.com/88250/lute/ast"
  23. "github.com/88250/lute/parse"
  24. "github.com/gorilla/css/scanner"
  25. "github.com/siyuan-note/logging"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. "github.com/vanng822/css"
  28. )
  29. func fillThemeStyleVar(tree *parse.Tree) {
  30. themeStyles := getThemeStyleVar(Conf.Appearance.ThemeLight)
  31. if 1 > len(themeStyles) {
  32. return
  33. }
  34. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  35. if !entering {
  36. return ast.WalkContinue
  37. }
  38. for _, ial := range n.KramdownIAL {
  39. if "style" != ial[0] {
  40. continue
  41. }
  42. styleSheet := css.Parse(ial[1])
  43. buf := bytes.Buffer{}
  44. for _, r := range styleSheet.GetCSSRuleList() {
  45. styles := getStyleVarName(r.Style.Selector)
  46. for style, name := range styles {
  47. buf.WriteString(style)
  48. buf.WriteString(": ")
  49. value := themeStyles[name]
  50. if "" == value {
  51. // 回退为变量
  52. buf.WriteString("var(")
  53. buf.WriteString(name)
  54. buf.WriteString(")")
  55. } else {
  56. buf.WriteString(value)
  57. }
  58. buf.WriteString("; ")
  59. }
  60. }
  61. if 0 < buf.Len() {
  62. ial[1] = strings.TrimSpace(buf.String())
  63. }
  64. }
  65. return ast.WalkContinue
  66. })
  67. }
  68. func getStyleVarName(value *css.CSSValue) (ret map[string]string) {
  69. ret = map[string]string{}
  70. var start, end int
  71. var style, name string
  72. for i, t := range value.Tokens {
  73. if scanner.TokenIdent == t.Type && 0 == start {
  74. style = strings.TrimSpace(t.Value)
  75. continue
  76. }
  77. if scanner.TokenFunction == t.Type && "var(" == t.Value {
  78. start = i
  79. continue
  80. }
  81. if scanner.TokenChar == t.Type && ")" == t.Value {
  82. end = i
  83. if 0 < start && 0 < end {
  84. for _, tt := range value.Tokens[start+1 : end] {
  85. name += tt.Value
  86. }
  87. name = strings.TrimSpace(name)
  88. }
  89. start, end = 0, 0
  90. ret[style] = name
  91. style, name = "", ""
  92. }
  93. }
  94. return
  95. }
  96. func getThemeStyleVar(theme string) (ret map[string]string) {
  97. ret = map[string]string{}
  98. data, err := os.ReadFile(filepath.Join(util.ThemesPath, theme, "theme.css"))
  99. if nil != err {
  100. logging.LogErrorf("read theme [%s] css file failed: %s", theme, err)
  101. return
  102. }
  103. styleSheet := css.Parse(string(data))
  104. for _, rule := range styleSheet.GetCSSRuleList() {
  105. for _, style := range rule.Style.Styles {
  106. ret[style.Property] = strings.TrimSpace(style.Value.Text())
  107. // 如果两个短横线开头 CSS 解析器有问题,--b3-theme-primary: #3575f0; 会被解析为 -b3-theme-primary:- #3575f0
  108. // 这里两种解析都放到结果中
  109. ret["-"+style.Property] = strings.TrimSpace(strings.TrimPrefix(style.Value.Text(), "-"))
  110. }
  111. }
  112. return
  113. }