-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipeconverter.pl
executable file
·93 lines (62 loc) · 2.65 KB
/
recipeconverter.pl
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
#!/usr/bin/perl
#RecipeConverter.pl v 1.0
#Converts recipes written in a standard human readable form into LaTEX code that can then be converted to a PDF or image
#Get the name of the file
$input=$ARGV[0];
print "INPUT:$input\n";
if (!$input){$input='chocolatecake.txt';}
open RECIPE, "<$input";
$inStep=0;#if 0, we're awaiting a label line; otherwise a list of ingredients
#Read it line by line, and add line to $notes or %instructions
while (<RECIPE>){
$line=$_;
($line, $junk) = split(/\s*#/, $line);
chomp $line;
if ($line=~ s/^\*//g){ #if it starts with a hash, it's a note
$notes=$notes . $line . "\\\\";
}
elsif($line =~ /\:/ && $inStep==0){ #if it contains a colon, and follows a newline, it's a label & instruction
$unrecordedData=1; #record that we've started reading actual instructions
($step, $instruction)=split(':', $line);
chomp $instruction;
@instructionLines=split('//', $instruction); #Convert `//` to newlines that will work in the table
if ($instructions>1) { $instruction = "\\begin{tabular}{l}" . join('//', @instructionLines) . "\\end{tabular}}" }
$inStep=1;
}
elsif( !($line =~ /\S/) ){ #if it's just whitespace, a step has ended or we haven't yet started
if($unrecordedData==1){
#Collapse what we've previously read one line of LaTEX
$markedup{$step}='\left. \begin{array}{ll} ';
foreach (@ingredients) {$markedup{$step} .= " \\mbox{" . $_ . "} \\\\ ";}
$markedup{$step} = substr $markedup{$step}, 0, -3; #rip off the terminal newline, as we don't need it
$markedup{$step} .= "& \\\\ \\end{array}\\right\\} \\begin{tabular}{l}" . $instruction . '\end{tabular}}';
#clear variables ready for the next step
$inStep=0;
@ingredients=();
$unrecordedData=0;
}
}
else{ #otherwise, its an ingredient
push @ingredients, $line;
}
}
close INPUTFILE;
#Smoosh each step together; look through ech entry in %markedup, replacing and symbols with their marked-up expansions
#we want to replace every instance of '\mbox{<A>}' with $markedup{A}, where A is an alphanumerical string
while ( $complete != 1){
$complete=1;
while(($key, $value) = each(%markedup)) {
if( $markedup{$key} =~ /\\mbox\{\<(\w)\>\}/){
$complete=0;
($before, $after)=split('\mbox{<' . $1 . ">}", $markedup{$key});
$before = substr $before, 0, -1;
$markedup{$key} = $before . $markedup{$1} . $after;
delete($markedup{$1}); #we no longer need the inserted markup, as each step can only be directly referenced once
}
}
}
#We print out the note as a title
chomp($notes);
print "\\textbf{$notes}\\vspace{0.4cm}\n\n" if $notes;
#and the actual instructions:
print values %markedup;