File Coverage

blib/lib/LaTeX/Writer/Simple.pm
Criterion Covered Total %
statement 55 57 96.4
branch 20 20 100.0
condition 4 6 66.6
subroutine 13 15 86.6
pod 2 2 100.0
total 94 100 94.0


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