-
Notifications
You must be signed in to change notification settings - Fork 1
/
md2protobuf
executable file
·124 lines (103 loc) · 3.8 KB
/
md2protobuf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl
###############################################################################
###
### md2protobuf - Extract protobuf definitions from Markdown files.
###
### USAGE: md2protobuf <markdown.md> <output.proto>
### Extract protobuf definitions from markdown.md into output.proto.
###
### output.proto is only created if protobuf definitions are actually found in
### the Markdown file.
###
###
### Copyright (C) 2017, Andy Bennett, Crown Copyright (Government Digital Service).
###
### Permission is hereby granted, free of charge, to any person obtaining a
### copy of this software and associated documentation files (the "Software"),
### to deal in the Software without restriction, including without limitation
### the rights to use, copy, modify, merge, publish, distribute, sublicense,
### and/or sell copies of the Software, and to permit persons to whom the
### Software is furnished to do so, subject to the following conditions:
###
### The above copyright notice and this permission notice shall be included in
### all copies or substantial portions of the Software.
###
### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
### THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
### FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
### DEALINGS IN THE SOFTWARE.
###
### Andy Bennett <[email protected]>, 2017/08/04
###
###############################################################################
use strict;
use warnings;
use POSIX qw(strftime);
use File::Basename;
my $flag = 0;
my $lineno = 0;
my $infile = undef; # The file we want to read from.
my $infh = undef; # The handle for the file we are reading from.
my $outfile = undef; # The file we want to write to.
my $openfile = undef; # The file we are writing to.
my $openfh = undef; # The handle for $openfile.
my $section = undef; # The current Markdown section.
sub usage {
my $me = basename($0);
print STDERR "USAGE: $me: <markdown.md> <output.proto>\n";
print STDERR "Extract protobuf definitions from markdown.md into output.proto.\n";
print STDERR "\n";
print STDERR "output.proto is only created if protobuf definitions are actually found in\n";
print STDERR "the Markdown file.\n";
print STDERR "\n\n";
}
(@ARGV == 2) or die usage();
$infile = $ARGV[0];
$outfile = $ARGV[1];
sub close_file {
if ($openfh) {
print $openfh "\n" . ("//" x 40) . "\n\n";
close $openfh;
}
}
sub ensure_open {
if ((!$openfile) or ($openfile ne $outfile)) {
my $now = strftime "%FT%TZ", gmtime;
close_file();
open($openfh, ">", $outfile) or die "Could not open file '$outfile' $!";
print $openfh ("//" x 40) . "\n";
print $openfh "// AUTOGENERATED by md2protobf\n";
print $openfh "// AUTOGENERATED ***DO NOT EDIT***\n";
print $openfh "// AUTOGENERATED from " . $infile . " on ";
print $openfh $now . "\n";
print $openfh ("//" x 40) . "\n";
print $openfh "\n\n";
$openfile = $outfile;
}
}
# Main Program
open($infh, "<", $infile) or die "Could not open file '$infile' $!";
while (my $line = <$infh>) {
$lineno = $lineno + 1;
# When we find the end of a code section, stop outputting lines.
if (($line =~ /```/) and ($flag == 1)) {
print $openfh "\n";
$flag = 0;
}
# Output lines whilst inside a code section.
if ($flag == 1) {
print $openfh $line;
}
# When we find the beginning of a code section, start outputting subsequent lines.
if ($line =~ /```protobuf/) {
ensure_open();
$flag = 1;
print $openfh "\n";
print $openfh "// " . $infile . ":" . $lineno . "\n";
}
}
close_file();
exit 0;