Skip to content

Commit 6ae5311

Browse files
committed
Use custom perl script for code embedding
1 parent 7753e7b commit 6ae5311

8 files changed

Lines changed: 250 additions & 191 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env perl
2+
use strict;
3+
use warnings;
4+
5+
# Ensure filename is given
6+
die "Usage: $0 <markdown-file>\n" unless @ARGV == 1;
7+
my $filename = $ARGV[0];
8+
9+
# Read the entire file into @lines
10+
open(my $fh, '<', $filename) or die "Could not open '$filename' for reading: $!\n";
11+
my @lines = <$fh>;
12+
close($fh);
13+
14+
my @output;
15+
my $i = 0;
16+
17+
while ($i < @lines) {
18+
my $line = $lines[$i];
19+
20+
if ($line =~ /^```([\w+-]+):(.+)\s*$/) {
21+
my ($lang, $filepath) = ($1, $2);
22+
23+
# Ensure file exists
24+
die "Error: File '$filepath' not found\n" unless -e $filepath;
25+
26+
# Open the included file and read contents
27+
open(my $code_fh, '<', $filepath) or die "Cannot read '$filepath': $!\n";
28+
my @code_lines = <$code_fh>;
29+
close($code_fh);
30+
31+
# Insert code block
32+
push @output, "```$lang:$filepath\n";
33+
push @output, @code_lines;
34+
chomp($output[-1]); # Prevent extra newline before closing ```
35+
push @output, "\n```\n";
36+
37+
# Skip to closing ```
38+
$i++;
39+
while ($i < @lines && $lines[$i] !~ /^```/) {
40+
$i++;
41+
}
42+
$i++; # Skip the closing ```
43+
} else {
44+
push @output, $line;
45+
$i++;
46+
}
47+
}
48+
49+
# Write the modified content back to the original file
50+
open(my $out_fh, '>', $filename) or die "Could not open '$filename' for writing: $!\n";
51+
print $out_fh @output;
52+
close($out_fh);

.github/workflows/update-readme.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v4
1616

17-
- name: Convert tabs to spaces for README
17+
- name: Convert tabs to spaces for README.md embedding
1818
run: find examples -type f -name "*.go" -exec sed -i 's/\t/ /g' {} +
1919

20-
- uses: kvankova/code-embedder@v1.1.2
20+
- name: Run embed-code-in-markdown.pl
21+
run: |
22+
./.github/workflows/embed-code-in-markdown.pl README.md
23+
git add README.md
24+
if git commit -m "Embedding code in README.md"; then
25+
branchName=${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}
26+
git push https://x-access-token:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY.git "$branchName"
27+
fi
2128
env:
2229
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

examples/mark/main.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package main
22

33
import (
4-
"log"
4+
"log"
55

6-
"github.com/neiser/go-nagini/command"
7-
"github.com/neiser/go-nagini/flag"
6+
"github.com/neiser/go-nagini/command"
7+
"github.com/neiser/go-nagini/flag"
88
)
99

1010
func main() {
11-
var (
12-
name = "Harry"
13-
iAmVoldemort bool
14-
)
15-
command.New().
16-
Flag(flag.String(&name, flag.NotEmpty), flag.RegisterOptions{
17-
Name: "name",
18-
}).
19-
Flag(flag.String(&name, flag.NotEmpty), flag.RegisterOptions{
20-
Name: "nickname",
21-
}).
22-
Flag(flag.Bool(&iAmVoldemort), flag.RegisterOptions{
23-
Name: "i-am-voldemort",
24-
}).
25-
MarkFlagsMutuallyExclusive(&name, &iAmVoldemort).
26-
Run(func() error {
27-
switch {
28-
case iAmVoldemort:
29-
log.Print("My name is Voldemort!")
30-
case name != "":
31-
log.Printf("My name is %s", name)
32-
}
33-
return nil
34-
}).
35-
Execute()
11+
var (
12+
name = "Harry"
13+
iAmVoldemort bool
14+
)
15+
command.New().
16+
Flag(flag.String(&name, flag.NotEmpty), flag.RegisterOptions{
17+
Name: "name",
18+
}).
19+
Flag(flag.String(&name, flag.NotEmpty), flag.RegisterOptions{
20+
Name: "nickname",
21+
}).
22+
Flag(flag.Bool(&iAmVoldemort), flag.RegisterOptions{
23+
Name: "i-am-voldemort",
24+
}).
25+
MarkFlagsMutuallyExclusive(&name, &iAmVoldemort).
26+
Run(func() error {
27+
switch {
28+
case iAmVoldemort:
29+
log.Print("My name is Voldemort!")
30+
case name != "":
31+
log.Printf("My name is %s", name)
32+
}
33+
return nil
34+
}).
35+
Execute()
3636
}

examples/simple/main.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
package main
22

33
import (
4-
"log"
4+
"log"
55

6-
"github.com/neiser/go-nagini/command"
7-
"github.com/neiser/go-nagini/flag"
6+
"github.com/neiser/go-nagini/command"
7+
"github.com/neiser/go-nagini/flag"
88
)
99

1010
type (
11-
Wand string
11+
Wand string
1212
)
1313

1414
func main() {
15-
var (
16-
myName string
17-
favoriteWand Wand = "elder"
18-
iAmVoldemort bool
19-
)
20-
command.New().
21-
Flag(flag.String(&myName, flag.NotEmpty), flag.RegisterOptions{
22-
Name: "my-name",
23-
Required: true,
24-
}).
25-
Flag(flag.String(&favoriteWand, flag.NotEmptyTrimmed), flag.RegisterOptions{
26-
Name: "favorite-wand",
27-
Usage: "Specify magic wand",
28-
}).
29-
Flag(flag.Bool(&iAmVoldemort), flag.RegisterOptions{
30-
Name: "i-am-voldemort",
31-
}).
32-
Run(func() error {
33-
if iAmVoldemort {
34-
return command.WithExitCodeError{ExitCode: 66}
35-
}
36-
log.Printf("I'm %s and my favorite wand is '%s'", myName, favoriteWand)
37-
return nil
38-
}).
39-
Execute()
15+
var (
16+
myName string
17+
favoriteWand Wand = "elder"
18+
iAmVoldemort bool
19+
)
20+
command.New().
21+
Flag(flag.String(&myName, flag.NotEmpty), flag.RegisterOptions{
22+
Name: "my-name",
23+
Required: true,
24+
}).
25+
Flag(flag.String(&favoriteWand, flag.NotEmptyTrimmed), flag.RegisterOptions{
26+
Name: "favorite-wand",
27+
Usage: "Specify magic wand",
28+
}).
29+
Flag(flag.Bool(&iAmVoldemort), flag.RegisterOptions{
30+
Name: "i-am-voldemort",
31+
}).
32+
Run(func() error {
33+
if iAmVoldemort {
34+
return command.WithExitCodeError{ExitCode: 66}
35+
}
36+
log.Printf("I'm %s and my favorite wand is '%s'", myName, favoriteWand)
37+
return nil
38+
}).
39+
Execute()
4040
}

examples/slice/main.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package main
22

33
import (
4-
"log"
5-
"strconv"
4+
"log"
5+
"strconv"
66

7-
"github.com/neiser/go-nagini/command"
8-
"github.com/neiser/go-nagini/flag"
7+
"github.com/neiser/go-nagini/command"
8+
"github.com/neiser/go-nagini/flag"
99
)
1010

1111
func main() {
12-
var (
13-
someInts []int
14-
)
15-
command.New().
16-
Flag(flag.NewSlice(&someInts, flag.ParseSliceOf(strconv.Atoi)), flag.RegisterOptions{
17-
Name: "some-ints",
18-
Required: true,
19-
}).
20-
Run(func() error {
21-
log.Printf("Got integers: '%v'", someInts)
22-
return nil
23-
}).
24-
Execute()
12+
var (
13+
someInts []int
14+
)
15+
command.New().
16+
Flag(flag.NewSlice(&someInts, flag.ParseSliceOf(strconv.Atoi)), flag.RegisterOptions{
17+
Name: "some-ints",
18+
Required: true,
19+
}).
20+
Run(func() error {
21+
log.Printf("Got integers: '%v'", someInts)
22+
return nil
23+
}).
24+
Execute()
2525
}

examples/subcommand/main.go

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
package main
22

33
import (
4-
"errors"
5-
"log"
4+
"errors"
5+
"log"
66

7-
"github.com/neiser/go-nagini/command"
8-
"github.com/neiser/go-nagini/flag"
7+
"github.com/neiser/go-nagini/command"
8+
"github.com/neiser/go-nagini/flag"
99
)
1010

1111
var ErrCannotUseMagic = errors.New("cannot use magic")
1212

1313
func main() {
14-
var (
15-
useMagic bool
16-
)
17-
command.New().
18-
Flag(flag.Bool(&useMagic), flag.RegisterOptions{
19-
Name: "use-magic",
20-
Usage: "Use some magic, c'mon",
21-
Persistent: true,
22-
}).
23-
AddCommands(
24-
command.New().
25-
Use("muggle").
26-
Short("A person which cannot use magic").
27-
Run(func() error {
28-
if useMagic {
29-
return command.WithExitCodeError{
30-
ExitCode: 21,
31-
Wrapped: ErrCannotUseMagic,
32-
}
33-
}
34-
return nil
35-
}),
36-
command.New().
37-
Use("wizard").
38-
Short("A person which may use magic").
39-
Run(func() error {
40-
if useMagic {
41-
log.Printf("Abracadabra!")
42-
}
43-
return nil
44-
}),
45-
).
46-
AddPersistentPreRun(func() error {
47-
log.Printf("Will always run!")
48-
return nil
49-
}).
50-
AddPersistentPreRun(func() error {
51-
log.Printf("Will also run!")
52-
return nil
53-
}).
54-
Execute()
14+
var (
15+
useMagic bool
16+
)
17+
command.New().
18+
Flag(flag.Bool(&useMagic), flag.RegisterOptions{
19+
Name: "use-magic",
20+
Usage: "Use some magic, c'mon",
21+
Persistent: true,
22+
}).
23+
AddCommands(
24+
command.New().
25+
Use("muggle").
26+
Short("A person which cannot use magic").
27+
Run(func() error {
28+
if useMagic {
29+
return command.WithExitCodeError{
30+
ExitCode: 21,
31+
Wrapped: ErrCannotUseMagic,
32+
}
33+
}
34+
return nil
35+
}),
36+
command.New().
37+
Use("wizard").
38+
Short("A person which may use magic").
39+
Run(func() error {
40+
if useMagic {
41+
log.Printf("Abracadabra!")
42+
}
43+
return nil
44+
}),
45+
).
46+
AddPersistentPreRun(func() error {
47+
log.Printf("Will always run!")
48+
return nil
49+
}).
50+
AddPersistentPreRun(func() error {
51+
log.Printf("Will also run!")
52+
return nil
53+
}).
54+
Execute()
5555
}

0 commit comments

Comments
 (0)