| 1 | // Copyright 2015 Matthew Holt and The Caddy 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 | |
| 15 | package caddyfile |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "io" |
| 20 | "slices" |
| 21 | "strings" |
| 22 | "unicode" |
| 23 | ) |
| 24 | |
| 25 | // Format formats the input Caddyfile to a standard, nice-looking |
| 26 | // appearance. It works by reading each rune of the input and taking |
| 27 | // control over all the bracing and whitespace that is written; otherwise, |
| 28 | // words, comments, placeholders, and escaped characters are all treated |
| 29 | // literally and written as they appear in the input. |
| 30 | func Format(input []byte) []byte { |
| 31 | input = bytes.TrimSpace(input) |
| 32 | |
| 33 | out := new(bytes.Buffer) |
| 34 | rdr := bytes.NewReader(input) |
| 35 | |
| 36 | type heredocState int |
| 37 | |
| 38 | const ( |
| 39 | heredocClosed heredocState = 0 |
| 40 | heredocOpening heredocState = 1 |
| 41 | heredocOpened heredocState = 2 |
| 42 | ) |
| 43 | |
| 44 | var ( |
| 45 | last rune // the last character that was written to the result |
| 46 | |
| 47 | space = true // whether current/previous character was whitespace (beginning of input counts as space) |
| 48 | beginningOfLine = true // whether we are at beginning of line |
| 49 | |
| 50 | openBrace bool // whether current word/token is or started with open curly brace |
| 51 | openBraceWritten bool // if openBrace, whether that brace was written or not |
| 52 | openBraceSpace bool // whether there was a non-newline space before open brace |
| 53 | |
| 54 | newLines int // count of newlines consumed |
| 55 | |
| 56 | comment bool // whether we're in a comment |
| 57 | quotes string // encountered quotes ('', '`', '"', '"`', '`"') |
| 58 | escaped bool // whether current char is escaped |
| 59 | |
| 60 | heredoc heredocState // whether we're in a heredoc |
| 61 | heredocEscaped bool // whether heredoc is escaped |
| 62 | heredocMarker []rune |
| 63 | heredocClosingMarker []rune |
| 64 | |
| 65 | nesting int // indentation level |
| 66 | |
| 67 | currentToken strings.Builder |
| 68 | currentLineFirstToken string |
| 69 | previousLineWasTopLevelImport bool |
| 70 | openBraceOwnLine bool |
| 71 | ) |
| 72 | |
| 73 | finishToken := func() { |
| 74 | if currentToken.Len() == 0 { |
| 75 | return |
| 76 | } |
| 77 | if currentLineFirstToken == "" { |
| 78 | currentLineFirstToken = currentToken.String() |
| 79 | } |
| 80 | currentToken.Reset() |
| 81 | } |
| 82 | |
| 83 | finishLine := func() { |
| 84 | finishToken() |
| 85 | if currentLineFirstToken != "" { |
| 86 | previousLineWasTopLevelImport = nesting == 0 && currentLineFirstToken == "import" |
| 87 | } else if !openBrace || !openBraceOwnLine || openBraceWritten { |
| 88 | previousLineWasTopLevelImport = false |
| 89 | } |
| 90 | currentLineFirstToken = "" |
| 91 | } |
| 92 | |
| 93 | write := func(ch rune) { |
| 94 | out.WriteRune(ch) |
| 95 | last = ch |
| 96 | } |
| 97 | |
| 98 | indent := func() { |
| 99 | for tabs := nesting; tabs > 0; tabs-- { |
| 100 | write('\t') |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | nextLine := func() { |
| 105 | write('\n') |
| 106 | beginningOfLine = true |
| 107 | } |
| 108 | |
| 109 | for { |
| 110 | ch, _, err := rdr.ReadRune() |
| 111 | if err != nil { |
| 112 | if err == io.EOF { |
| 113 | break |
| 114 | } |
| 115 | panic(err) |
| 116 | } |
| 117 | // detect whether we have the start of a heredoc |
| 118 | if quotes == "" && (heredoc == heredocClosed && !heredocEscaped) && |
| 119 | space && last == '<' && ch == '<' { |
| 120 | write(ch) |
| 121 | heredoc = heredocOpening |
| 122 | space = false |
| 123 | continue |
| 124 | } |
| 125 | |
| 126 | if heredoc == heredocOpening { |
| 127 | if ch == '\n' { |
| 128 | if len(heredocMarker) > 0 && heredocMarkerRegexp.MatchString(string(heredocMarker)) { |
| 129 | heredoc = heredocOpened |
| 130 | } else { |
| 131 | heredocMarker = nil |
| 132 | heredoc = heredocClosed |
| 133 | nextLine() |
| 134 | continue |
| 135 | } |
| 136 | write(ch) |
| 137 | continue |
| 138 | } |
| 139 | if unicode.IsSpace(ch) { |
| 140 | // a space means it's just a regular token and not a heredoc |
| 141 | heredocMarker = nil |
| 142 | heredoc = heredocClosed |
| 143 | } else { |
| 144 | heredocMarker = append(heredocMarker, ch) |
| 145 | write(ch) |
| 146 | continue |
| 147 | } |
| 148 | } |
| 149 | // if we're in a heredoc, all characters are read&write as-is |
| 150 | if heredoc == heredocOpened { |
| 151 | heredocClosingMarker = append(heredocClosingMarker, ch) |
| 152 | if len(heredocClosingMarker) > len(heredocMarker)+1 { // We assert that the heredocClosingMarker is followed by a unicode.Space |
| 153 | heredocClosingMarker = heredocClosingMarker[1:] |
| 154 | } |
| 155 | // check if we're done |
| 156 | if unicode.IsSpace(ch) && slices.Equal(heredocClosingMarker[:len(heredocClosingMarker)-1], heredocMarker) { |
| 157 | heredocMarker = nil |
| 158 | heredocClosingMarker = nil |
| 159 | heredoc = heredocClosed |
| 160 | } else { |
| 161 | write(ch) |
| 162 | if ch == '\n' { |
| 163 | heredocClosingMarker = heredocClosingMarker[:0] |
| 164 | } |
| 165 | continue |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if last == '<' && space { |
| 170 | space = false |
| 171 | } |
| 172 | |
| 173 | if comment { |
| 174 | if ch == '\n' { |
| 175 | comment = false |
| 176 | space = true |
| 177 | nextLine() |
| 178 | continue |
| 179 | } else { |
| 180 | write(ch) |
| 181 | continue |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if !escaped && ch == '\\' { |
| 186 | if space { |
| 187 | write(' ') |
| 188 | space = false |
| 189 | } |
| 190 | write(ch) |
| 191 | escaped = true |
| 192 | continue |
| 193 | } |
| 194 | |
| 195 | if escaped { |
| 196 | if ch == '<' { |
| 197 | heredocEscaped = true |
| 198 | } |
| 199 | write(ch) |
| 200 | escaped = false |
| 201 | continue |
| 202 | } |
| 203 | |
| 204 | if ch == '`' { |
| 205 | switch quotes { |
| 206 | case "\"`": |
| 207 | quotes = "\"" |
| 208 | case "`": |
| 209 | quotes = "" |
| 210 | case "\"": |
| 211 | quotes = "\"`" |
| 212 | default: |
| 213 | quotes = "`" |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if quotes == "\"" { |
| 218 | if ch == '"' { |
| 219 | quotes = "" |
| 220 | } |
| 221 | write(ch) |
| 222 | continue |
| 223 | } |
| 224 | |
| 225 | if ch == '"' { |
| 226 | switch quotes { |
| 227 | case "": |
| 228 | if space { |
| 229 | quotes = "\"" |
| 230 | } |
| 231 | case "`\"": |
| 232 | quotes = "`" |
| 233 | case "\"`": |
| 234 | quotes = "" |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if strings.Contains(quotes, "`") { |
| 239 | if ch == '`' && space && !beginningOfLine { |
| 240 | write(' ') |
| 241 | } |
| 242 | write(ch) |
| 243 | space = false |
| 244 | continue |
| 245 | } |
| 246 | |
| 247 | if unicode.IsSpace(ch) { |
| 248 | finishToken() |
| 249 | space = true |
| 250 | heredocEscaped = false |
| 251 | if ch == '\n' { |
| 252 | finishLine() |
| 253 | newLines++ |
| 254 | } |
| 255 | continue |
| 256 | } |
| 257 | spacePrior := space |
| 258 | space = false |
| 259 | |
| 260 | ////////////////////////////////////////////////////////// |
| 261 | // I find it helpful to think of the formatting loop in two |
| 262 | // main sections; by the time we reach this point, we |
| 263 | // know we are in a "regular" part of the file: we know |
| 264 | // the character is not a space, not in a literal segment |
| 265 | // like a comment or quoted, it's not escaped, etc. |
| 266 | ////////////////////////////////////////////////////////// |
| 267 | |
| 268 | if ch == '#' { |
| 269 | comment = true |
| 270 | } |
| 271 | |
| 272 | if openBrace && spacePrior && !openBraceWritten { |
| 273 | if nesting == 0 && last == '}' { |
| 274 | nextLine() |
| 275 | nextLine() |
| 276 | } |
| 277 | |
| 278 | openBrace = false |
| 279 | if openBraceOwnLine && previousLineWasTopLevelImport { |
| 280 | if last != '\n' { |
| 281 | nextLine() |
| 282 | } |
| 283 | indent() |
| 284 | } else if beginningOfLine { |
| 285 | indent() |
| 286 | } else if !openBraceSpace || !unicode.IsSpace(last) { |
| 287 | write(' ') |
| 288 | } |
| 289 | write('{') |
| 290 | openBraceWritten = true |
| 291 | openBraceOwnLine = false |
| 292 | nextLine() |
| 293 | newLines = 0 |
| 294 | // prevent infinite nesting from ridiculous inputs (issue #4169) |
| 295 | if nesting < 10 { |
| 296 | nesting++ |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | switch { |
| 301 | case ch == '{': |
| 302 | finishToken() |
| 303 | openBrace = true |
| 304 | openBraceSpace = spacePrior && !beginningOfLine |
| 305 | openBraceOwnLine = newLines > 0 |
| 306 | if openBraceSpace && newLines == 0 { |
| 307 | write(' ') |
| 308 | } |
| 309 | openBraceWritten = false |
| 310 | if quotes == "`" { |
| 311 | write('{') |
| 312 | openBraceWritten = true |
| 313 | openBraceOwnLine = false |
| 314 | continue |
| 315 | } |
| 316 | continue |
| 317 | |
| 318 | case ch == '}' && (spacePrior || !openBrace): |
| 319 | finishToken() |
| 320 | if quotes == "`" { |
| 321 | write('}') |
| 322 | continue |
| 323 | } |
| 324 | if last != '\n' { |
| 325 | nextLine() |
| 326 | } |
| 327 | if nesting > 0 { |
| 328 | nesting-- |
| 329 | } |
| 330 | indent() |
| 331 | write('}') |
| 332 | newLines = 0 |
| 333 | continue |
| 334 | } |
| 335 | |
| 336 | if newLines > 2 { |
| 337 | newLines = 2 |
| 338 | } |
| 339 | for i := 0; i < newLines; i++ { |
| 340 | nextLine() |
| 341 | } |
| 342 | newLines = 0 |
| 343 | if beginningOfLine { |
| 344 | indent() |
| 345 | } |
| 346 | if nesting == 0 && last == '}' && beginningOfLine { |
| 347 | nextLine() |
| 348 | nextLine() |
| 349 | } |
| 350 | |
| 351 | if !beginningOfLine && spacePrior { |
| 352 | write(' ') |
| 353 | } |
| 354 | |
| 355 | if openBrace && !openBraceWritten { |
| 356 | write('{') |
| 357 | openBraceWritten = true |
| 358 | } |
| 359 | |
| 360 | if spacePrior && ch == '<' { |
| 361 | space = true |
| 362 | } |
| 363 | |
| 364 | currentToken.WriteRune(ch) |
| 365 | write(ch) |
| 366 | |
| 367 | beginningOfLine = false |
| 368 | } |
| 369 | |
| 370 | // the Caddyfile does not need any leading or trailing spaces, but... |
| 371 | trimmedResult := bytes.TrimSpace(out.Bytes()) |
| 372 | |
| 373 | // ...Caddyfiles should, however, end with a newline because |
| 374 | // newlines are significant to the syntax of the file |
| 375 | return append(trimmedResult, '\n') |
| 376 | } |