Skip to content

Commit

Permalink
Use page not file
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
  • Loading branch information
jdbaldry committed Jun 27, 2024
1 parent ab43c05 commit 136e5b7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 69 deletions.
97 changes: 51 additions & 46 deletions tools/transformer/directives.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 34 additions & 17 deletions tools/transformer/hack/generate-directives/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,46 @@ func generateMarkers(outputPath string) error {
g.Printf("package " + "main")
g.Printf("\n")

g.Printf("const (\n")
{
g.Printf("const (\n")

for directive := range directives {
switch directive {
case "file":
g.Printf("%sIntroStartMarker = `%s`\n", directive, marker(directive, []string{"intro.md"}, true))
g.Printf("%sIntroEndMarker = `%s`\n", directive, marker(directive, []string{"intro.md"}, false))
for directive := range directives {
switch directive {
case "page":
g.Printf("%sIntroStartMarker = `%s`\n", directive, marker(directive, []string{"intro.md"}, true))
g.Printf("%sIntroEndMarker = `%s`\n", directive, marker(directive, []string{"intro.md"}, false))

g.Printf("%sFinishStartMarker = `%s`\n", directive, marker(directive, []string{"finish.md"}, true))
g.Printf("%sFinishEndMarker = `%s`\n", directive, marker(directive, []string{"finish.md"}, false))
g.Printf("%sFinishStartMarker = `%s`\n", directive, marker(directive, []string{"finish.md"}, true))
g.Printf("%sFinishEndMarker = `%s`\n", directive, marker(directive, []string{"finish.md"}, false))

for i := 1; i < 20; i++ {
stepFilename := fmt.Sprintf("step%d.md", i)
g.Printf("%sStep%dStartMarker = `%s`\n", directive, i, marker(directive, []string{stepFilename}, true))
g.Printf("%sStep%dEndMarker = `%s`\n", directive, i, marker(directive, []string{stepFilename}, false))
default:
g.Printf("%sStartMarker = `%s`\n", directive, marker(directive, nil, true))
g.Printf("%sEndMarker = `%s`\n", directive, marker(directive, nil, false))
}
default:
g.Printf("%sStartMarker = `%s`\n", directive, marker(directive, nil, true))
g.Printf("%sEndMarker = `%s`\n", directive, marker(directive, nil, false))
}

g.Printf(")\n")
}

g.Printf(")\n")
{
const maxSteps = 20

g.Printf("var pageStepStartMarkers = [%d]string{\n", maxSteps)

for i := 1; i < maxSteps; i++ {
stepFilename := fmt.Sprintf("step%d.md", i)
g.Printf("\"%s\",\n", marker("page", []string{stepFilename}, true))
}
g.Printf("}\n")

g.Printf("var pageStepEndMarkers = [%d]string{\n", maxSteps)

for i := 1; i < maxSteps; i++ {
stepFilename := fmt.Sprintf("step%d.md", i)
g.Printf("\"%s\",\n", marker("page", []string{stepFilename}, false))
}
g.Printf("}\n")
}

src, err := g.format()
if err != nil {
Expand Down Expand Up @@ -113,11 +130,11 @@ const (
var directives = map[string][]string{
"copy": {},
"exec": {},
"file": {"FILENAME"},
"finish": {},
"ignore": {},
"include": {},
"intro": {},
"page": {"FILENAME"},
}

func marker(directive string, args []string, isStart bool) string {
Expand Down
12 changes: 6 additions & 6 deletions tools/transformer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ func transform(srcFilePath, dstDirPath string) error {
wroteFinish bool
)

if bytes.Contains(data, []byte(fileIntroStartMarker)) {
if err := writeFile(dstDirPath, "intro.md", append(transformers, util.Prioritized(&StepTransformer{StartMarker: fileIntroStartMarker, EndMarker: fileIntroEndMarker}, 0)), renderer, data); err != nil {
if bytes.Contains(data, []byte(pageIntroStartMarker)) {
if err := writeFile(dstDirPath, "intro.md", append(transformers, util.Prioritized(&StepTransformer{StartMarker: pageIntroStartMarker, EndMarker: pageIntroEndMarker}, 0)), renderer, data); err != nil {
return err
}

wroteIntro = true
}

if bytes.Contains(data, []byte(fileFinishStartMarker)) {
if err := writeFile(dstDirPath, "finish.md", append(transformers, util.Prioritized(&StepTransformer{StartMarker: fileFinishStartMarker, EndMarker: fileFinishEndMarker}, 0)), renderer, data); err != nil {
if bytes.Contains(data, []byte(pageFinishStartMarker)) {
if err := writeFile(dstDirPath, "finish.md", append(transformers, util.Prioritized(&StepTransformer{StartMarker: pageFinishStartMarker, EndMarker: pageFinishEndMarker}, 0)), renderer, data); err != nil {
return err
}

Expand All @@ -164,8 +164,8 @@ func transform(srcFilePath, dstDirPath string) error {
)

for i := 1; i <= 20; i++ {
startMarker := fmt.Sprintf(`<!-- INTERACTIVE file step%d.md START -->`, i)
endMarker := fmt.Sprintf(`<!-- INTERACTIVE file step%d.md END -->`, i)
startMarker := pageStepStartMarkers[i-1]
endMarker := pageStepEndMarkers[i-1]

if regexp.MustCompile(startMarker).Match(data) {
steps++
Expand Down

0 comments on commit 136e5b7

Please sign in to comment.