parser.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2016 Google Inc. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to writing, software distributed
  8. // under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
  9. // CONDITIONS OF ANY KIND, either express or implied.
  10. //
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package embedmd
  14. import (
  15. "bufio"
  16. "fmt"
  17. "io"
  18. "strings"
  19. )
  20. type commandRunner func(io.Writer, *command) error
  21. func process(out io.Writer, in io.Reader, run commandRunner) error {
  22. s := &countingScanner{bufio.NewScanner(in), 0}
  23. state := parsingText
  24. var err error
  25. for state != nil {
  26. state, err = state(out, s, run)
  27. if err != nil {
  28. return fmt.Errorf("%d: %v", s.line, err)
  29. }
  30. }
  31. if err := s.Err(); err != nil {
  32. return fmt.Errorf("%d: %v", s.line, err)
  33. }
  34. return nil
  35. }
  36. type countingScanner struct {
  37. *bufio.Scanner
  38. line int
  39. }
  40. func (c *countingScanner) Scan() bool {
  41. b := c.Scanner.Scan()
  42. if b {
  43. c.line++
  44. }
  45. return b
  46. }
  47. type textScanner interface {
  48. Text() string
  49. Scan() bool
  50. }
  51. type state func(io.Writer, textScanner, commandRunner) (state, error)
  52. func parsingText(out io.Writer, s textScanner, run commandRunner) (state, error) {
  53. if !s.Scan() {
  54. return nil, nil // end of file, which is fine.
  55. }
  56. switch line := s.Text(); {
  57. case strings.HasPrefix(line, "[embedmd]:#"):
  58. return parsingCmd, nil
  59. case strings.HasPrefix(line, "```"):
  60. return codeParser{print: true}.parse, nil
  61. default:
  62. fmt.Fprintln(out, s.Text())
  63. return parsingText, nil
  64. }
  65. }
  66. func parsingCmd(out io.Writer, s textScanner, run commandRunner) (state, error) {
  67. line := s.Text()
  68. fmt.Fprintln(out, line)
  69. args := line[strings.Index(line, "#")+1:]
  70. cmd, err := parseCommand(args)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if err := run(out, cmd); err != nil {
  75. return nil, err
  76. }
  77. if !s.Scan() {
  78. return nil, nil // end of file, which is fine.
  79. }
  80. if strings.HasPrefix(s.Text(), "```") {
  81. return codeParser{print: false}.parse, nil
  82. }
  83. fmt.Fprintln(out, s.Text())
  84. return parsingText, nil
  85. }
  86. type codeParser struct{ print bool }
  87. func (c codeParser) parse(out io.Writer, s textScanner, run commandRunner) (state, error) {
  88. if c.print {
  89. fmt.Fprintln(out, s.Text())
  90. }
  91. if !s.Scan() {
  92. return nil, fmt.Errorf("unbalanced code section")
  93. }
  94. if !strings.HasPrefix(s.Text(), "```") {
  95. return c.parse, nil
  96. }
  97. // print the end of the code section if needed and go back to parsing text.
  98. if c.print {
  99. fmt.Fprintln(out, s.Text())
  100. }
  101. return parsingText, nil
  102. }