mob: Use 4 decimals in SI output
[sunny256-utils.git] / md-header
blob419708038f18921e0da61c3d82716343a56ce023
1 #!/usr/bin/env perl
3 #=======================================================================
4 # md-header
5 # File ID: b9e646d2-ae65-11e5-837d-fefdb24f8e10
7 # Add Vim fold markers to headers in Commonmark/Markdown
9 # Character set: UTF-8
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'help' => 0,
24 'quiet' => 0,
25 'remove' => 0,
26 'verbose' => 0,
27 'version' => 0,
31 our $progname = $0;
32 $progname =~ s/^.*\/(.*?)$/$1/;
33 our $VERSION = '0.2.1';
35 Getopt::Long::Configure('bundling');
36 GetOptions(
38 'help|h' => \$Opt{'help'},
39 'quiet|q+' => \$Opt{'quiet'},
40 'remove|r' => \$Opt{'remove'},
41 'verbose|v+' => \$Opt{'verbose'},
42 'version' => \$Opt{'version'},
44 ) || die("$progname: Option error. Use -h for help.\n");
46 $Opt{'verbose'} -= $Opt{'quiet'};
47 $Opt{'help'} && usage(0);
48 if ($Opt{'version'}) {
49 print_version();
50 exit(0);
53 exit(main());
55 sub main {
56 # {{{
57 my $Retval = 0;
58 my $FB = "\x7b\x7b\x7b";
59 my $FB_regex = "\\\x7b\\\x7b\\\x7b";
61 while (my $Line = <>) {
62 chomp($Line);
63 if ($Line =~ /^(#+)/) {
64 if ($Opt{'remove'}) {
65 $Line =~ s/ ?<!-- $FB_regex\d? -->//;
66 } else {
67 my $marks = $1;
68 my $level = length($marks);
69 if ($Line =~ /<!-- $FB_regex\d? -->/) {
70 $Line =~ s/ ?<!-- $FB_regex\d? -->/ <!-- ${FB}$level -->/;
71 } else {
72 $Line .= " <!-- ${FB}$level -->";
76 print("$Line\n");
79 return $Retval;
80 # }}}
81 } # main()
83 sub print_version {
84 # Print program version {{{
85 print("$progname $VERSION\n");
86 return;
87 # }}}
88 } # print_version()
90 sub usage {
91 # Send the help message to stdout {{{
92 my $Retval = shift;
94 if ($Opt{'verbose'}) {
95 print("\n");
96 print_version();
98 print(<<"END");
100 Add Vim folds to '#*' headers in Commonmark/Markdown.
102 Usage: $progname [options] [file [files [...]]]
104 Options:
106 -h, --help
107 Show this help.
108 -q, --quiet
109 Be more quiet. Can be repeated to increase silence.
110 -r, --remove
111 Remove folds from header lines instead of adding them.
112 -v, --verbose
113 Increase level of verbosity. Can be repeated.
114 --version
115 Print version information.
118 exit($Retval);
119 # }}}
120 } # usage()
122 sub msg {
123 # Print a status message to stderr based on verbosity level {{{
124 my ($verbose_level, $Txt) = @_;
126 if ($Opt{'verbose'} >= $verbose_level) {
127 print(STDERR "$progname: $Txt\n");
129 return;
130 # }}}
131 } # msg()
133 __END__
135 # This program is free software; you can redistribute it and/or modify
136 # it under the terms of the GNU General Public License as published by
137 # the Free Software Foundation; either version 2 of the License, or (at
138 # your option) any later version.
140 # This program is distributed in the hope that it will be useful, but
141 # WITHOUT ANY WARRANTY; without even the implied warranty of
142 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
143 # See the GNU General Public License for more details.
145 # You should have received a copy of the GNU General Public License
146 # along with this program.
147 # If not, see L<http://www.gnu.org/licenses/>.
149 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :