escape.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //Copyright 2013 GoGraphviz Authors
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. //Unless required by applicable law or agreed to in writing, software
  10. //distributed under the License is distributed on an "AS IS" BASIS,
  11. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. //See the License for the specific language governing permissions and
  13. //limitations under the License.
  14. package gographviz
  15. import (
  16. "fmt"
  17. "strings"
  18. "text/template"
  19. "unicode"
  20. )
  21. // Escape is just a Graph that escapes some strings when required.
  22. type Escape struct {
  23. *Graph
  24. }
  25. // NewEscape returns a graph which will try to escape some strings when required
  26. func NewEscape() *Escape {
  27. return &Escape{NewGraph()}
  28. }
  29. func isHTML(s string) bool {
  30. if len(s) == 0 {
  31. return false
  32. }
  33. ss := strings.TrimSpace(s)
  34. if ss[0] != '<' {
  35. return false
  36. }
  37. count := 0
  38. for _, c := range ss {
  39. if c == '<' {
  40. count++
  41. }
  42. if c == '>' {
  43. count--
  44. }
  45. }
  46. if count == 0 {
  47. return true
  48. }
  49. return false
  50. }
  51. func isLetter(ch rune) bool {
  52. return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' ||
  53. ch >= 0x80 && unicode.IsLetter(ch) && ch != 'ε'
  54. }
  55. func isID(s string) bool {
  56. i := 0
  57. pos := false
  58. for _, c := range s {
  59. if i == 0 {
  60. if !isLetter(c) {
  61. return false
  62. }
  63. pos = true
  64. }
  65. if unicode.IsSpace(c) {
  66. return false
  67. }
  68. if c == '-' {
  69. return false
  70. }
  71. if c == '/' {
  72. return false
  73. }
  74. if c == '.' {
  75. return false
  76. }
  77. i++
  78. }
  79. return pos
  80. }
  81. func isDigit(ch rune) bool {
  82. return '0' <= ch && ch <= '9' || ch >= 0x80 && unicode.IsDigit(ch)
  83. }
  84. func isNumber(s string) bool {
  85. state := 0
  86. for _, c := range s {
  87. if state == 0 {
  88. if isDigit(c) || c == '.' {
  89. state = 2
  90. } else if c == '-' {
  91. state = 1
  92. } else {
  93. return false
  94. }
  95. } else if state == 1 {
  96. if isDigit(c) || c == '.' {
  97. state = 2
  98. }
  99. } else if c != '.' && !isDigit(c) {
  100. return false
  101. }
  102. }
  103. return (state == 2)
  104. }
  105. func isStringLit(s string) bool {
  106. if !strings.HasPrefix(s, `"`) || !strings.HasSuffix(s, `"`) {
  107. return false
  108. }
  109. var prev rune
  110. for _, r := range s[1 : len(s)-1] {
  111. if r == '"' && prev != '\\' {
  112. return false
  113. }
  114. prev = r
  115. }
  116. return true
  117. }
  118. func esc(s string) string {
  119. if len(s) == 0 {
  120. return s
  121. }
  122. if isHTML(s) {
  123. return s
  124. }
  125. ss := strings.TrimSpace(s)
  126. if ss[0] == '<' {
  127. return fmt.Sprintf("\"%s\"", strings.Replace(s, "\"", "\\\"", -1))
  128. }
  129. if isID(s) {
  130. return s
  131. }
  132. if isNumber(s) {
  133. return s
  134. }
  135. if isStringLit(s) {
  136. return s
  137. }
  138. return fmt.Sprintf("\"%s\"", template.HTMLEscapeString(s))
  139. }
  140. func escAttrs(attrs map[string]string) map[string]string {
  141. newAttrs := make(map[string]string)
  142. for k, v := range attrs {
  143. newAttrs[esc(k)] = esc(v)
  144. }
  145. return newAttrs
  146. }
  147. // SetName sets the graph name and escapes it, if needed.
  148. func (escape *Escape) SetName(name string) error {
  149. return escape.Graph.SetName(esc(name))
  150. }
  151. // AddPortEdge adds an edge with ports and escapes the src, dst and attrs, if needed.
  152. func (escape *Escape) AddPortEdge(src, srcPort, dst, dstPort string, directed bool, attrs map[string]string) error {
  153. return escape.Graph.AddPortEdge(esc(src), srcPort, esc(dst), dstPort, directed, escAttrs(attrs))
  154. }
  155. // AddEdge adds an edge and escapes the src, dst and attrs, if needed.
  156. func (escape *Escape) AddEdge(src, dst string, directed bool, attrs map[string]string) error {
  157. return escape.AddPortEdge(src, "", dst, "", directed, attrs)
  158. }
  159. // AddNode adds a node and escapes the parentGraph, name and attrs, if needed.
  160. func (escape *Escape) AddNode(parentGraph string, name string, attrs map[string]string) error {
  161. return escape.Graph.AddNode(esc(parentGraph), esc(name), escAttrs(attrs))
  162. }
  163. // AddAttr adds an attribute and escapes the parentGraph, field and value, if needed.
  164. func (escape *Escape) AddAttr(parentGraph string, field, value string) error {
  165. return escape.Graph.AddAttr(esc(parentGraph), esc(field), esc(value))
  166. }
  167. // AddSubGraph adds a subgraph and escapes the parentGraph, name and attrs, if needed.
  168. func (escape *Escape) AddSubGraph(parentGraph string, name string, attrs map[string]string) error {
  169. return escape.Graph.AddSubGraph(esc(parentGraph), esc(name), escAttrs(attrs))
  170. }
  171. // IsNode returns whether the, escaped if needed, name is a node in the graph.
  172. func (escape *Escape) IsNode(name string) bool {
  173. return escape.Graph.IsNode(esc(name))
  174. }
  175. // IsSubGraph returns whether the, escaped if needed, name is a subgraph in the grahp.
  176. func (escape *Escape) IsSubGraph(name string) bool {
  177. return escape.Graph.IsSubGraph(esc(name))
  178. }