Debian Codegraph @

texmf-dist/doc/latex/songbook/contrib/crd2sb/crd2sb

raw
1 #!/usr/local/bin/perl
2
3 # crd2sb - convert chordpro files to songbook (LaTeX style)
4
5 # Usage: crd2sb filename.crd [filename2.crd] . . [filenamen.crd]
6
7 # The input files are "*.crd" and the output files are "*.tex" (in the
8 # current directory).
9
10 # $Id: crd2sb,v 1.9 1997/12/05 19:25:05 abel Exp $
11
12 # (C)Copyright 1997 Abel Chow <[email protected]>
13
14 # You may distribute under the terms of the Artistic License,
15 # as specified in the relnotes.txt file.
16
17 use File::Basename;
18
19 $debuglevel = 0;
20 $date = localtime;
21
22 sub Init
23 {
24 $title = "";
25 $key = "";
26 $copyright = "Unknown";
27 $wandm = "Unknown";
28 $sref = "";
29 $ccli = "";
30 $lyrics = "";
31
32 $nextenv = "SBVerse";
33 }
34
35 sub EndSong
36 {
37 unless ($title) {
38 print STDERR "$file:$.: No title defined for song.\n";
39 Init();
40 return;
41 }
42 $ccli = "\\NotCCLIed" if $copyright =~ /\bPublic Domain\b/;
43
44 print STDERR "$file:$.: key undefined for \"$title\".\n" unless $key;
45 print STDERR "$file:$.: copyright info undefined for \"$title\".\n" if $copyright eq "Unknown";
46 print STDERR "$file:$.: author undefined for \"$title\".\n" if $wandm eq "Unknown";
47 # print STDERR "$file:$.: scripture ref. undefined for \"$title\".\n" unless $sref;
48 print STDERR "$file:$.: CCLI status undefined for \"$title\".\n" unless $ccli;
49
50 if ($env) {
51 DoEnd($env);
52 }
53
54 print "% This file is machine generated. Do not edit.\n";
55 print "% crd2sb $file\n";
56 print "% created on $date\n";
57 print "\\begin{song}{$title}{$key}\n";
58 print " {$copyright}\n {$wandm}\n {$sref}\n {$ccli}\n";
59 print "$lyrics";
60 print "\\end{song}\n";
61 Init();
62 }
63
64 sub DoNewSong
65 {
66 EndSong();
67 Init();
68 }
69
70 sub DoSubTitle
71 {
72 ($_) = @_;
73 # figure out if it's a copyright
74 if (s/\(C\)// + s/\bCopyright\b//) {
75 s/^\s+//;
76 s/\s+$//;
77 $copyright = $_;
78 return;
79 }
80 if (/Public Domain/) {
81 $copyright = $_;
82 return;
83 }
84 s/^W&M://;
85 s/^\s+//;
86 s/\s+$//;
87 $wandm = $_;
88 }
89
90 sub DoComment
91 {
92 ($_) = @_;
93 print STDERR "$file:$.: > $_\n" if $debuglevel > 0;
94 if (($p) = /^tex:\s*(.*)/) {
95 $lyrics .= "$p\n";
96 return;
97 }
98 if (($p) = /^note:\s*(.*)/) {
99 $lyrics .= "\\SBMargNote{$p}\n";
100 return;
101 }
102 if (($p) = /^intro:\s*(.*)/) {
103 $p =~ s/#/\$\\sharp\$/g;
104 $p =~ s/b/\$\\flat\$/g;
105 $p =~ s/\|/\$|\$/g;
106 $lyrics .= "\\SBIntro{\\ChFont $p}\n";
107 return;
108 }
109 if (/^section:/) {
110 $nextenv = "SBSection";
111 return;
112 }
113 # figure out if there's a CCLI ref anywhere
114 $ccli = "\\CCLIed" if /\bccli\b/i;
115 # key setting
116 if (/^\s*key:\s*(.*)/) {
117 my($k) = $1;
118 if ($k !~ /^[A-G]\S*/) {
119 print STDERR "$file:$.: invalid key $k\n";
120 }
121 $k =~ s/#/\$\\sharp\$/g;
122 $k =~ s/b/\$\\flat\$/g;
123 $key = $k;
124 return;
125 }
126 $sref = $1 if /^\s*sref:\s*(.*)/;
127 }
128
129 sub DoBegin
130 {
131 my($myenv) = @_;
132 printf STDERR "$file:$.: missing env $env" if $env;
133 $env = $myenv;
134 $lyrics .= "\\begin{$env}\n";
135 $first = 1;
136 }
137
138 sub DoEnd
139 {
140 print STDERR "$file:$.: mismatched quote\n" if $quote;
141 $quote = 0;
142 printf STDERR "$file:$.: missing env $env\n" unless ($env);
143 $lyrics .= "\\end{$env}\n";
144 $env = "";
145 }
146
147 unless (@ARGV) {
148 print "usage: crd2sb filename.crd [filename2.crd] ... [filenamen.crd]\n"
149 }
150
151 Init();
152 foreach $file (@ARGV) {
153 ($base, $path, $type) = fileparse($file, '\.crd');
154 unless ($type) {
155 print STDERR "$file: not a Chordpro (*.crd) file\n";
156 next;
157 }
158 $outfile = "$base.tex";
159 print STDERR "#### $file -> $outfile ####\n"
160 if $debuglevel > 0;
161 unless (open(INFILE, $file)) {
162 print STDERR "$file: $!\n";
163 exit;
164 }
165 close(STDOUT);
166 unless (open(STDOUT, ">$outfile")) {
167 print STDERR "$outfile: $!\n";
168 exit;
169 }
170 while (<INFILE>) {
171 if (/^#\s+(.*)/) {
172 DoComment($1);
173 next;
174 }
175 next if /^#/;
176 # directive
177 if (m/^\s*{([^\}:]+)(:\s*([^\}]*))?}/) {
178 # title
179 if ($1 eq "title" || $1 eq "t") {
180 $title = $3; next;
181 }
182 if ($1 eq "subtitle" || $1 eq "st") {
183 DoSubTitle($3); next;
184 }
185 if ($1 eq "comment" || $1 eq "c") {
186 DoComment($3); next;
187 }
188 # new_song
189 if ($1 eq "new_song" || $1 eq "ns") {
190 DoNewSong(); next;
191 }
192 if ($1 eq "start_of_chorus" || $1 eq "soc") {
193 DoBegin("SBChorus"); next;
194 }
195 if ($1 eq "end_of_chorus" || $1 eq "eoc") {
196 DoEnd("SBChorus"); next;
197 }
198 next;
199 }
200 if (/{/ || /}/) {
201 print STDERR "$file:$.: unmatched brace\n";
202 }
203
204 # strip blank spaces and comments
205 s/\s+/ /;
206 s/^\s//;
207 s/\s$//;
208 s/&/\\&/;
209 unless ($_) { # blank line
210 DoEnd() if $env;
211 next;
212 }
213 while (/"/) { # "
214 if ($quote) {
215 $quote = 0 if (s/"/\'\'/); # " }
216 }
217 else {
218 $quote = 1 if (s/"/\`\`/); # " }
219 }
220 }
221 # protect the parens in chords
222 s/(\[[^\[\]]*)\(/$1</g;
223 s/\)([^\[\]]*\])/>$1/g;
224 s/\(/{\\em /g;
225 s/\)/}/g;
226 tr/\<\>/\(\)/;
227
228 s/\|/\$|\$/g;
229 s/\[([^\[\]]*)\]-/\\Ch{$1}{\\SBem}/g;
230 s/\[([^\[\]]*)\]([A-Za-z0-9,;:.!"'`]+)/\\Ch{$1}{$2}/g; #'`
231 s/\[([^\[\]]*)\]/\\Ch{$1}{}/g;#}}
232 s/\.\.\./\\ldots{}/g;
233
234 unless ($env) {
235 DoBegin($nextenv) unless $env;
236 $nextenv = "SBVerse";
237 }
238 $lyrics .= "\n" if $env && !$first;
239 $first = 0;
240 $lyrics .= "$_\n";
241 }
242 DoNewSong();
243 }