Skip to content

Commit

Permalink
Support more keywords (#50)
Browse files Browse the repository at this point in the history
* Add clarification test case

* Add support for more marker keywords

* Add test cases

* Update to use more keywords
  • Loading branch information
rytswd authored Sep 3, 2021
1 parent 0f6a43a commit f8ee32c
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 47 deletions.
4 changes: 4 additions & 0 deletions internal/cli/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func TestGenerateStdout(t *testing.T) {
inputFile: "../../testdata/markdown/import-with-exporter-before.md",
wantFile: "../../testdata/markdown/import-with-exporter-updated.md",
},
"yaml with exporter": {
inputFile: "../../testdata/yaml/demo-before.yaml",
wantFile: "../../testdata/yaml/demo-updated.yaml",
},
"yaml with exporter and align": {
inputFile: "../../testdata/yaml/align-with-exporter-before.yaml",
wantFile: "../../testdata/yaml/align-with-exporter-updated.yaml",
Expand Down
8 changes: 4 additions & 4 deletions internal/marker/marker_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ var (
// Example:
// <!-- == imptr: some_importer_name / begin from: ./file.txt#2~22 == -->
// <!-- == imptr: some_importer_name / end == -->
ImporterMarkerMarkdown = `<!-- == imptr: (?P<importer_name>\S+) \/ (?P<importer_marker>begin|end)(?P<importer_option>.*) == -->`
ImporterMarkerMarkdown = `<!-- == (imptr|import|importer|i): (?P<importer_name>\S+) \/ (?P<importer_marker>begin|end)(?P<importer_option>.*) == -->`

// ImporterMarkerYAML is the annotation used for importer to find match.
ImporterMarkerYAML = `(?P<importer_marker_indentation>\s*)# == imptr: (?P<importer_name>\S+) \/ (?P<importer_marker>begin|end)(?P<importer_option>.*) ==`
ImporterMarkerYAML = `(?P<importer_marker_indentation>\s*)# == (imptr|import|importer|i): (?P<importer_name>\S+) \/ (?P<importer_marker>begin|end)(?P<importer_option>.*) ==`

// OptionFilePathIndicator is the pattern used for parsing Importer file options.
OptionFilePathIndicator = `from: (?P<importer_target_path>\S+)\s*\#(?P<importer_target_detail>[0-9a-zA-Z,-_\~]+)\s?`
Expand All @@ -28,7 +28,7 @@ var (
// You can import this content by providing option such as:
// ./file_path.txt#[simple_instruction]
// <!-- == export: simple_instruction / end == -->
ExporterMarkerMarkdown = `<!-- == export: (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) == -->`
ExporterMarkerMarkdown = `<!-- == (exptr|export|exporter|e): (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) == -->`

// ExporterMarkerYAML is the marker used to indicate how a file can export
// specific sections.
Expand All @@ -39,5 +39,5 @@ var (
// # == export: random_data / begin ==
// random-data: this is exported
// # == export: random_data / end ==
ExporterMarkerYAML = `(?P<export_marker_indent>\s*)# == export: (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) ==`
ExporterMarkerYAML = `(?P<export_marker_indent>\s*)# == (exptr|export|exporter|e): (?P<export_marker_name>\S+) \/ (?P<exporter_marker_condition>begin|end) ==`
)
9 changes: 9 additions & 0 deletions internal/regexpplus/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func TestFindNamedSubgroups(t *testing.T) {
"a": "d", // later one takes precedence
},
},
"2 different named groups and 1 unnamed group": {
targetLine: "abc def ghi jkl mno",
regexpInput: `(?P<a>a.*).*(?P<d>d)(.*)`,
want: map[string]string{
"": "ef ghi jkl mno",
"a": "abc ",
"d": "d",
},
},
"2 unnamed groups": {
targetLine: "abc def ghi jkl mno",
regexpInput: `(ab).*(de)`,
Expand Down
4 changes: 2 additions & 2 deletions testdata/yaml/align-with-exporter-before.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ data:
indentation:
for:
demo:
# == imptr: abc-tree / begin from: ./snippet-with-exporter.yaml#[long-tree] indent: align ==
# == importer: abc-tree / begin from: ./snippet-with-exporter.yaml#[long-tree] indent: align ==
anything: here
would: be
purged: by Importer
# == imptr: abc-tree / end ==
# == importer: abc-tree / end ==

some-data:
description: |
Expand Down
4 changes: 2 additions & 2 deletions testdata/yaml/align-with-exporter-purged.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ data:
indentation:
for:
demo:
# == imptr: abc-tree / begin from: ./snippet-with-exporter.yaml#[long-tree] indent: align ==
# == imptr: abc-tree / end ==
# == importer: abc-tree / begin from: ./snippet-with-exporter.yaml#[long-tree] indent: align ==
# == importer: abc-tree / end ==

some-data:
description: |
Expand Down
4 changes: 2 additions & 2 deletions testdata/yaml/align-with-exporter-updated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data:
indentation:
for:
demo:
# == imptr: abc-tree / begin from: ./snippet-with-exporter.yaml#[long-tree] indent: align ==
# == importer: abc-tree / begin from: ./snippet-with-exporter.yaml#[long-tree] indent: align ==
a:
b:
c:
Expand All @@ -19,7 +19,7 @@ data:
i:
j:
k: {}
# == imptr: abc-tree / end ==
# == importer: abc-tree / end ==

some-data:
description: |
Expand Down
4 changes: 2 additions & 2 deletions testdata/yaml/demo-before.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: Demo of YAML Importer
# == imptr: description / begin from: ./snippet-description.yaml#[for-demo] ==
# == import: description / begin from: ./snippet-description.yaml#[for-demo] ==
dummy: This will be replaced
# == imptr: description / end ==
# == import: description / end ==
4 changes: 2 additions & 2 deletions testdata/yaml/demo-purged.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
title: Demo of YAML Importer
# == imptr: description / begin from: ./snippet-description.yaml#[for-demo] ==
# == imptr: description / end ==
# == import: description / begin from: ./snippet-description.yaml#[for-demo] ==
# == import: description / end ==
5 changes: 2 additions & 3 deletions testdata/yaml/demo-updated.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
title: Demo of YAML Importer
# == imptr: description / begin from: ./snippet-description.yaml#[for-demo] ==
# == import: description / begin from: ./snippet-description.yaml#[for-demo] ==
description: |
This demonstrates how importing YAML snippet is made possible, without
changing YAML handling at all.
# == imptr: description / end ==
# == import: description / end ==
12 changes: 6 additions & 6 deletions testdata/yaml/k8s-color-svc-before.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ spec:
spec:
serviceAccountName: color-svc
containers:
# == imptr: latest-color-svc / begin from: ./snippet-k8s-color-svc.yaml#[latest-svc] indent: align ==
# == i: latest-color-svc / begin from: ./snippet-k8s-color-svc.yaml#[latest-svc] indent: align ==
- image: docker.io/rytswd/color-svc:latest
name: color-svc
command:
- color-svc
ports:
- containerPort: 8800
# == imptr: latest-color-svc / end ==
# == i: latest-color-svc / end ==

env:
# == imptr: color-svc-default-envs / begin from: ./snippet-k8s-color-svc.yaml#[basic-envs] indent: align ==
# == imptr: color-svc-default-envs / end ==
# == i: color-svc-default-envs / begin from: ./snippet-k8s-color-svc.yaml#[basic-envs] indent: align ==
# == i: color-svc-default-envs / end ==
- name: DISABLE_RED
value: "true"
- name: DISABLE_GREEN
Expand All @@ -52,7 +52,7 @@ spec:
- name: DISABLE_YELLOW
value: "true"

# == imptr: resource-footprint / begin from: ./snippet-k8s-resource.yaml#[min-resource] indent: align ==
# == i: resource-footprint / begin from: ./snippet-k8s-resource.yaml#[min-resource] indent: align ==
data: |
this will be purged
# == imptr: resource-footprint / end ==
# == i: resource-footprint / end ==
12 changes: 6 additions & 6 deletions testdata/yaml/k8s-color-svc-purged.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ spec:
spec:
serviceAccountName: color-svc
containers:
# == imptr: latest-color-svc / begin from: ./snippet-k8s-color-svc.yaml#[latest-svc] indent: align ==
# == imptr: latest-color-svc / end ==
# == i: latest-color-svc / begin from: ./snippet-k8s-color-svc.yaml#[latest-svc] indent: align ==
# == i: latest-color-svc / end ==

env:
# == imptr: color-svc-default-envs / begin from: ./snippet-k8s-color-svc.yaml#[basic-envs] indent: align ==
# == imptr: color-svc-default-envs / end ==
# == i: color-svc-default-envs / begin from: ./snippet-k8s-color-svc.yaml#[basic-envs] indent: align ==
# == i: color-svc-default-envs / end ==
- name: DISABLE_RED
value: "true"
- name: DISABLE_GREEN
Expand All @@ -46,5 +46,5 @@ spec:
- name: DISABLE_YELLOW
value: "true"

# == imptr: resource-footprint / begin from: ./snippet-k8s-resource.yaml#[min-resource] indent: align ==
# == imptr: resource-footprint / end ==
# == i: resource-footprint / begin from: ./snippet-k8s-resource.yaml#[min-resource] indent: align ==
# == i: resource-footprint / end ==
12 changes: 6 additions & 6 deletions testdata/yaml/k8s-color-svc-updated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ spec:
spec:
serviceAccountName: color-svc
containers:
# == imptr: latest-color-svc / begin from: ./snippet-k8s-color-svc.yaml#[latest-svc] indent: align ==
# == i: latest-color-svc / begin from: ./snippet-k8s-color-svc.yaml#[latest-svc] indent: align ==
- image: docker.io/rytswd/color-svc:latest
name: color-svc
command:
- color-svc
ports:
- containerPort: 8800
# == imptr: latest-color-svc / end ==
# == i: latest-color-svc / end ==

env:
# == imptr: color-svc-default-envs / begin from: ./snippet-k8s-color-svc.yaml#[basic-envs] indent: align ==
# == i: color-svc-default-envs / begin from: ./snippet-k8s-color-svc.yaml#[basic-envs] indent: align ==
- name: ENABLE_DELAY
value: "true"
- name: DELAY_DURATION_MILLISECOND
value: "500"
- name: ENABLE_CORS
value: "true"
# == imptr: color-svc-default-envs / end ==
# == i: color-svc-default-envs / end ==
- name: DISABLE_RED
value: "true"
- name: DISABLE_GREEN
Expand All @@ -58,12 +58,12 @@ spec:
- name: DISABLE_YELLOW
value: "true"

# == imptr: resource-footprint / begin from: ./snippet-k8s-resource.yaml#[min-resource] indent: align ==
# == i: resource-footprint / begin from: ./snippet-k8s-resource.yaml#[min-resource] indent: align ==
resources:
requests:
cpu: 10m
memory: 10Mi
limits:
cpu: 30m
memory: 30Mi
# == imptr: resource-footprint / end ==
# == i: resource-footprint / end ==
16 changes: 8 additions & 8 deletions testdata/yaml/snippet-k8s-color-svc.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
containers:
# == export: latest-svc / begin ==
# == e: latest-svc / begin ==
- image: docker.io/rytswd/color-svc:latest
name: color-svc
command:
- color-svc
ports:
- containerPort: 8800
# == export: latest-svc / end ==
# == e: latest-svc / end ==

# == export: v0.1.0 / begin ==
# == e: v0.1.0 / begin ==
- image: docker.io/rytswd/color-svc:0.1.0
name: color-svc
command:
- color-svc
ports:
- containerPort: 8800
# == export: v0.1.0 / end ==
# == e: v0.1.0 / end ==

envs:
# == export: basic-envs / begin ==
# == e: basic-envs / begin ==
- name: ENABLE_DELAY
value: "true"
- name: DELAY_DURATION_MILLISECOND
value: "500"
- name: ENABLE_CORS
value: "true"
# == export: basic-envs / end ==
# == e: basic-envs / end ==

# == export: disable-all-colours / begin ==
# == e: disable-all-colours / begin ==
- name: DISABLE_RED
value: "true"
- name: DISABLE_GREEN
Expand All @@ -36,4 +36,4 @@ envs:
value: "true"
- name: DISABLE_YELLOW
value: "true"
# == export: disable-all-colours / end ==
# == e: disable-all-colours / end ==
4 changes: 2 additions & 2 deletions testdata/yaml/snippet-k8s-resource.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
minimal:
# == export: min-resource / begin ==
# == exptr: min-resource / begin ==
resources:
requests:
cpu: 10m
memory: 10Mi
limits:
cpu: 30m
memory: 30Mi
# == export: min-resource / end ==
# == exptr: min-resource / end ==
4 changes: 2 additions & 2 deletions testdata/yaml/snippet-with-exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test-data:
# == export: metadata-only / end ==
# == export: sample-nested / end ==

# == export: long-tree / begin ==
# == exporter: long-tree / begin ==
a:
b:
c:
Expand All @@ -30,4 +30,4 @@ a:
i:
j:
k: {}
# == export: long-tree / end ==
# == exporter: long-tree / end ==

0 comments on commit f8ee32c

Please sign in to comment.