escape.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. i++
  75. }
  76. return pos
  77. }
  78. func isDigit(ch rune) bool {
  79. return '0' <= ch && ch <= '9' || ch >= 0x80 && unicode.IsDigit(ch)
  80. }
  81. func isNumber(s string) bool {
  82. state := 0
  83. for _, c := range s {
  84. if state == 0 {
  85. if isDigit(c) || c == '.' {
  86. state = 2
  87. } else if c == '-' {
  88. state = 1
  89. } else {
  90. return false
  91. }
  92. } else if state == 1 {
  93. if isDigit(c) || c == '.' {
  94. state = 2
  95. }
  96. } else if c != '.' && !isDigit(c) {
  97. return false
  98. }
  99. }
  100. return (state == 2)
  101. }
  102. func isStringLit(s string) bool {
  103. if !strings.HasPrefix(s, `"`) || !strings.HasSuffix(s, `"`) {
  104. return false
  105. }
  106. var prev rune
  107. for _, r := range s[1 : len(s)-1] {
  108. if r == '"' && prev != '\\' {
  109. return false
  110. }
  111. prev = r
  112. }
  113. return true
  114. }
  115. func esc(s string) string {
  116. if len(s) == 0 {
  117. return s
  118. }
  119. if isHTML(s) {
  120. return s
  121. }
  122. ss := strings.TrimSpace(s)
  123. if ss[0] == '<' {
  124. return fmt.Sprintf("\"%s\"", strings.Replace(s, "\"", "\\\"", -1))
  125. }
  126. if isID(s) {
  127. return s
  128. }
  129. if isNumber(s) {
  130. return s
  131. }
  132. if isStringLit(s) {
  133. return s
  134. }
  135. return fmt.Sprintf("\"%s\"", template.HTMLEscapeString(s))
  136. }
  137. func escAttrs(attrs map[string]string) map[string]string {
  138. newAttrs := make(map[string]string)
  139. for k, v := range attrs {
  140. newAttrs[esc(k)] = esc(v)
  141. }
  142. return newAttrs
  143. }
  144. // SetName sets the graph name and escapes it, if needed.
  145. func (escape *Escape) SetName(name string) error {
  146. return escape.Graph.SetName(esc(name))
  147. }
  148. // AddPortEdge adds an edge with ports and escapes the src, dst and attrs, if needed.
  149. func (escape *Escape) AddPortEdge(src, srcPort, dst, dstPort string, directed bool, attrs map[string]string) error {
  150. return escape.Graph.AddPortEdge(esc(src), srcPort, esc(dst), dstPort, directed, escAttrs(attrs))
  151. }
  152. // AddEdge adds an edge and escapes the src, dst and attrs, if needed.
  153. func (escape *Escape) AddEdge(src, dst string, directed bool, attrs map[string]string) error {
  154. return escape.AddPortEdge(src, "", dst, "", directed, attrs)
  155. }
  156. // AddNode adds a node and escapes the parentGraph, name and attrs, if needed.
  157. func (escape *Escape) AddNode(parentGraph string, name string, attrs map[string]string) error {
  158. return escape.Graph.AddNode(esc(parentGraph), esc(name), escAttrs(attrs))
  159. }
  160. // AddAttr adds an attribute and escapes the parentGraph, field and value, if needed.
  161. func (escape *Escape) AddAttr(parentGraph string, field, value string) error {
  162. return escape.Graph.AddAttr(esc(parentGraph), esc(field), esc(value))
  163. }
  164. // AddSubGraph adds a subgraph and escapes the parentGraph, name and attrs, if needed.
  165. func (escape *Escape) AddSubGraph(parentGraph string, name string, attrs map[string]string) error {
  166. return escape.Graph.AddSubGraph(esc(parentGraph), esc(name), escAttrs(attrs))
  167. }
  168. // IsNode returns whether the, escaped if needed, name is a node in the graph.
  169. func (escape *Escape) IsNode(name string) bool {
  170. return escape.Graph.IsNode(esc(name))
  171. }
  172. // IsSubGraph returns whether the, escaped if needed, name is a subgraph in the grahp.
  173. func (escape *Escape) IsSubGraph(name string) bool {
  174. return escape.Graph.IsSubGraph(esc(name))
  175. }