File Coverage

blib/lib/LaTeX/Writer/Simple.pm
Criterion Covered Total %
statement 70 72 97.2
branch 20 20 100.0
condition 4 6 66.6
subroutine 14 16 87.5
pod 2 2 100.0
total 110 116 94.8


line stmt bran cond sub pod time code
1             package LaTeX::Writer::Simple;
2              
3 5     5   28942 use warnings;
  5         10  
  5         137  
4 5     5   24 use strict;
  5         9  
  5         153  
5              
6 5     5   29 use base 'Exporter';
  5         14  
  5         1445  
7             our $VERSION = '0.01';
8              
9             our @EXPORT;
10             our @EXPORT_OK = (qw/_c _e/);
11              
12             BEGIN {
13 5     5   16 @EXPORT = (qw/document p/);
14            
15             sub _def {
16 90     90   153 my ($name, $sub) = @_;
17            
18 5     5   30 no strict 'refs';
  5         7  
  5         1789  
19 90         142 my $x = "LaTeX::Writer::Simple::$name";
20 90         511 *$x = $sub;
21 90         5132 push @EXPORT, $name;
22             }
23            
24 5         27 my @nl_commands = (qw/part chapter section subsection subsubsection caption/);
25 5         25 for my $c (@nl_commands) {
26 30     5   136 _def($c, sub { _c($c, @_)."\n" });
  5         25  
27             }
28              
29 5         15 my @inline_commands = (qw/label Ref pageRef/);
30 5         13 for my $c (@inline_commands) {
31 15     0   80 _def($c, sub { _c($c, @_) });
  0         0  
32             }
33            
34 5         24 my %abbr_inline_commands = (it => 'textit',
35             bf => 'textbf',
36             tt => 'texttt');
37 5         18 for my $c (keys %abbr_inline_commands) {
38 15         27 my $cc = $abbr_inline_commands{$c};
39 15     3   84 _def($c, sub { _c($cc, @_) });
  3         10  
40 15     6   134 _def($cc, sub { _c($cc, @_) });
  6         25  
41             }
42            
43 5         18 my @envs = (qw/center flushleft flushright/);
44 5         9 for my $c (@envs) {
45 15     0   352 _def($c, sub { _e($c, @_) });
  0         0  
46             }
47            
48             }
49              
50              
51             =head1 NAME
52              
53             LaTeX::Writer::Simple - A module to help writing LaTeX file.
54              
55             =head1 SYNOPSIS
56              
57             LaTeX::Writer::Simple provides a programmatically interface to write LaTeX files
58             from within Perl.
59              
60             use LaTeX::Writer::Simple;
61              
62             my $foo = document( { -title => {Document Title},
63             })
64              
65             print $foo;
66              
67             =head1 EXPORT
68              
69             By default the module exports the following LaTeX functions:
70              
71             =head2 part
72              
73             =head2 chapter
74              
75             =head2 section
76              
77             =head2 subsection
78              
79             =head2 subsubsection
80              
81             =head2 caption
82              
83             =head2 texttt
84              
85             =head2 textit
86              
87             =head2 textbf
88              
89             =head2 it
90              
91             =head2 tt
92              
93             =head2 label
94              
95             =head2 Ref
96              
97             =head2 pageRef
98              
99             =head2 bf
100              
101             =head2 center
102              
103             =head2 flushleft
104              
105             =head2 flushright
106              
107             =head2 p
108              
109             =cut
110              
111             sub p {
112 3     3 1 19 return join("\n\n", @_)."\n\n";
113             }
114              
115             =head2 document
116              
117             =cut
118              
119             sub document {
120 4     4 1 8233 my $conf = { documentclass => 'article',
121             title => '',
122             date => '\today',
123             author => '',
124             maketitle => 1,
125             options => [],
126             packages => { url => 1,
127             graphicx => 1,
128             inputenc => ['utf8'],
129             babel => ['english']},
130             };
131              
132 4 100       74 $conf = _merge($conf, shift(@_)) if ref $_[0];
133            
134 4 100       15 my $ops = (ref($conf->{options}) eq "ARRAY")?join(",",@{$conf->{options}}):$conf->{options};
  3         10  
135 4         13 my $latex_document = "\\documentclass[$ops]\{$conf->{documentclass}}\n";
136              
137 4         6 for my $package_name (keys %{$conf->{packages}}) {
  4         16  
138 16 100       39 if (ref($conf->{packages}{$package_name}) eq "ARRAY") {
139 8         10 $latex_document .= "\\usepackage[".join(",",@{$conf->{packages}{$package_name}})."]{$package_name}\n";
  8         78  
140             } else {
141 8 100       24 if ($conf->{packages}{$package_name}) {
142 7         20 $latex_document .= "\\usepackage{$package_name}\n";
143             }
144             }
145             }
146              
147 4 100       17 if (ref($conf->{author}) eq "ARRAY") {
148 1         2 $conf->{author} = join(' \and ', @{$conf->{author}});
  1         4  
149             }
150 4         9 for (qw/title date author/) {
151 12         33 $latex_document .= "\\$_\{$conf->{$_}}\n";
152             }
153              
154 4         8 $latex_document .= "\\begin{document}\n";
155 4 100       12 $latex_document .= "\\maketitle\n" if $conf->{maketitle};
156 4         8 $latex_document .= join("\n", @_);
157 4         741 $latex_document .= "\\end{document}\n";
158 4         32 return $latex_document;
159             }
160              
161             ## Auxiliary functions...
162              
163             sub _c {
164 17     17   33 my $command = shift;
165 17 100       49 my $opt_args = (ref($_[0]) eq "ARRAY")?shift(@_):"";
166 17 100       133 my $args = @_?join("", map "{$_}", @_):"";
167            
168 17 100       56 $opt_args = '['.join(",",@$opt_args).']' if ($opt_args);
169            
170 17         102 return "\\$command$opt_args$args"
171             }
172              
173             sub _merge {
174 4     4   8 my ($defaults, $options) = @_;
175            
176 4         14 for my $key (keys %$options) {
177 9 100 66     59 if ((exists $defaults->{$key}) &&
      66        
178             (ref $defaults->{$key} eq "HASH") &&
179             (ref $options->{$key} eq "HASH")) {
180 1         26 $defaults->{$key} = _merge($defaults->{$key}, $options->{$key})
181             } else {
182 8         23 $defaults->{$key} = $options->{$key};
183             }
184             }
185 4         56 return $defaults;
186             }
187              
188             sub _e {
189 1     1   8 my $name = shift;
190 1         14 return "\\begin{$name}\n".join("\n",@_)."\n\\end{$name}"
191             }
192              
193             =head1 AUTHOR
194              
195             Alberto Simoes, C<< >>
196              
197             =head1 BUGS
198              
199             Please report any bugs or feature requests to C, or through
200             the web interface at L. I will be notified, and then you'll
201             automatically be notified of progress on your bug as I make changes.
202              
203             =head1 SUPPORT
204              
205             You can find documentation for this module with the perldoc command.
206              
207             perldoc LaTeX::Writer::Simple
208              
209              
210             You can also look for information at:
211              
212             =over 4
213              
214             =item * RT: CPAN's request tracker
215              
216             L
217              
218             =item * AnnoCPAN: Annotated CPAN documentation
219              
220             L
221              
222             =item * CPAN Ratings
223              
224             L
225              
226             =item * Search CPAN
227              
228             L
229              
230             =back
231              
232             =head1 COPYRIGHT & LICENSE
233              
234             Copyright 2008 Alberto Simoes, all rights reserved.
235              
236             This program is free software; you can redistribute it and/or modify it
237             under the same terms as Perl itself.
238              
239              
240             =cut
241              
242             1; # End of LaTeX::Writer::Simple