File: NewCDLayout.pl

package info (click to toggle)
tkcdlayout 0.5-0.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 76 kB
  • ctags: 13
  • sloc: sh: 263; perl: 219
file content (279 lines) | stat: -rwxr-xr-x 9,197 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/perl
# Script perl to generate a LaTeX file from the configuration file 
# (set with -config=...), then run latex, dvips and gv on this file.
# (use -psviewer=... to set another postscript visualizer)

sub usage {
  print STDERR "Usage : [perl] $0 -config=file [-psviewer=prog] [OPTIONS]

 'file' : config file generated by tkcdlayout or written manually.
 'prog' : the postscript visualizer you want to use.
            It can be \"gv -landscape\", kghostview, pageview...
 -update : no ps visualizer will be launched after the ps is generated.
            Use 'Update' button in the visualizer to see the new ps.
 -help : display this information
 -version : display version and author information
 NOTE : '--' options are supported too (ex: --help).
";
  exit 1;
}

sub version {
  print STDERR "$0 version 0.5. --help for usage.
Written by David Faure, faure\@kde.org, 1998. Licensed under GPL.
";
  exit 1;
}

sub fix {
  ($_) = @_;
  # Fixes LaTeX special handing of some special characters.
  s/_/\\_/g;			#  _ -> \_
  s/\#/\\\#/g; 			#  # -> \#
  s/&/\\&/g;			#  & -> \&
  return $_;
}

sub GetFiles {
  # List files ($maxdepth levels from $cddir)
  my ($cddir, $maxdepth, $showdirs, $showfiles, $exclude) = @_;
  my ($maxdepthOPT, $typeOPT, @lines);
  
  @lines=();
  if ($cddir ne "") { 
      $maxdepthOPT = ($maxdepth > 0) ? "-maxdepth " . $maxdepth : "";
        # -links 2 -> no directory with subdirectories.
      $typeOPT = ($showdirs eq "yes") ? "-type d -links 2" : "";
      $typeOPT .= " -o " if ($showdirs eq "yes" && $showfiles eq "yes");
      $typeOPT .= "-type f" if ($showfiles eq "yes");
      # Run a find command
      chdir $cddir or die "Can't cd to $cddir : $!";
      open (FIND,"find . $maxdepthOPT $typeOPT |");
      while (<FIND>)
	{
	  # Exclude "." and $exclude (if set)
	  unless (($exclude ne "" && /$exclude/) || /^.$/)
	    {
	      s/^.\///;		#  Remove ./ at the beginning of each line
	      print ;
	      &fix($_);
	      chomp;		# Remove \n
	      # Add space at the beginning and the LaTeX sign '\\' at the end
	      $_ = "        " . $_ . '\\\\' . "\n"; 
	      push @lines, $_;
	    }
	}
      close (FIND);
      @lines = sort @lines;		# Do we always want to sort ?
    }
  return @lines;
}

sub BeginLaTeX {
  my($Title, $rules) = @_;
  my($LeftRule, $RightRule);
  
  $LeftRule="";
  $RightRule="";
  if ($rules eq "yes") {
    if (length $Title > 21) { print "Title too big for left & right rules\n"; }
    else {
      $LeftRule='\rule{1cm}{0.1mm} ';
      $RightRule=' \rule{2cm}{0.1mm}';
    }
  }
  print LATEXFILE 
    '\documentclass[a4paper] {article}

\usepackage{t1enc}     % for 8bit chars (optional)
\usepackage{lscape}    % for landscape
\usepackage{newcent}   % PS police with serif=New Century (optional)
\usepackage{rotate}    % for rotate :)
\usepackage{vmargin}   % for setmarginsrb

\pagestyle{empty}      % no page numbers

\newcommand{\cdtitle}{' . &fix($Title) . '}
\newcommand{\leftrule}{' . $LeftRule . '}
\newcommand{\rightrule}{' . $RightRule . '}

\setmarginsrb{2cm}{2cm}{2cm}{2cm}{0pt}{0pt}{0pt}{1cm} % page margins
%            Left  Top Right Bot.

\begin{document}
\landscape{
';
}

sub beginframebox {
  my ($h, $w, $contents) = @_;
  print LATEXFILE '\framebox{\parbox[c]['.$h.'][t]{'.$w.'}{'.$contents;
}

sub endframebox {
  print LATEXFILE "}}";		# No \n here ! (Otherwise there is a space between the frames)
}

sub MakeTitle {
  my ($size) = @_;
  print LATEXFILE '    \begin{center}
      '.$size.'{\leftrule{}\rmfamily\itshape\cdtitle{}\rightrule{}\\\\}
    \end{center}'."\n";
  
}
sub DoLayout {
  # Do the LaTeX output for the layouts.
  # $page can be front, back, or both
  # $front_layout_type and $back_layout_type can be taf or btas
  # The rest is passed to GetFiles()
  my ($page, $front_layout_type, $back_layout_type, $subtitle, @getfiles_args) = @_;
  my (@lines, @pages, $p, @remaininglines);
  if ($page eq "both") { @pages=("front","back"); } else { @pages=($page); }
  if (($front_layout_type eq "taf") || ($back_layout_type eq "taf")) { @lines = &GetFiles(@getfiles_args); }
  my $height = "11.6cm";	# or 12 ???
  
  foreach $p (@pages) {		# Loop over the pages
    
    if ($p eq "back") {		# back : vertical title on the left
      $layout_type = $back_layout_type;
      &beginframebox($height,"1cm",'\rotate[l]{\cdtitle{}}');
      &endframebox();
      &beginframebox($height,"13.6cm","\n");
    } else {			# front : only a big frame
      $layout_type = $front_layout_type;
      &beginframebox($height,"12.2cm","\n");
    }
    if ($layout_type eq "taf")	# Title and files
      {
	print LATEXFILE '    \vspace{2mm}'."\n".'    \raggedright'."\n";
	&MakeTitle('\Huge');	# or \LARGE if \Huge is too big...
	# Write the first 24 lines, cut them if back (only 1 page)
	@remaininglines = &WriteLines(24, ($p eq "back") ? "cut" : "no", @lines);
	# TODO : if the title is really big and goes on 2 lines, then this
	#        should be 21 instead of 24. But how to check that ? What's
	#        more, if a line is too long, it wraps, eating another line !
      }
    elsif ($layout_type eq "btas") # Big title and subtitle
      {
	print LATEXFILE '\vspace{4cm} \raggedright'."\n";
	&MakeTitle('\Huge');	# Maximum size
	&Subtitle($subtitle);
      }
    if ($p eq "front")		# front : two pages. Let's do the second one.
      {
	&endframebox();
	&beginframebox($height,"12.2cm","\n");
	&WriteLines(28, "cut", @remaininglines) if ($layout_type eq "taf");
      }
    &endframebox();
    if ($p eq "back") {		# back : vertical title on the right
      &beginframebox($height,"1cm",'\rotate[r]{\cdtitle{}}');
      &endframebox();
    }
    if (($page eq "both") && ($p eq "front")) {
      print LATEXFILE '\newpage'."\n"; 
    }
  }				# foreach
}

sub WriteLines {
# Writes up to $N lines from @lines to the LaTeX file, in a box
# If more than $N lines, either cut them (if $cut)
# or send them back, for the next page
  my ($N, $cut, @lines) = @_;
  my $count = $#lines+1;
  if (($cut eq "cut") && ($count > $N)) { # Too many lines : remove some
    @lines = (@lines[0..$N-2], ' \ldots\\\\' . "\n");	  # And add '...'
    $count = $#lines+1; # should be == $N
  }
  print STDERR "N : $N  lines count : $count \n"; # debug output
  print STDERR "Last one is ".$lines[$#lines]; # debug output
  print LATEXFILE '    \vspace{5mm}
    \hspace{1cm}\parbox[c][10.9cm][t]{11cm}{\sffamily\raggedright\small{'."\n";
  my $lasttoprint = ($count < $N) ? $count-1 : $N-1 ;
  print LATEXFILE @lines[0..$lasttoprint] if ($lasttoprint >= 0);
  print STDERR "Lasttoprint : $lasttoprint\n"; # debug output
  print LATEXFILE '    }}'."\n"; # ends small and parbox
  if ($#lines >= $lasttoprint+1) { return @lines[$lasttoprint+1..$#lines]; } else { return (); }
}

sub Subtitle {
  my ($subtitle) = @_;
  # the \hspace doesn't work, but I left it because it allows two spaces
  # to be taken into account (the one before and the one after)    !!
  print LATEXFILE '   \vspace{3cm} \begin{flushright}\sffamily\LARGE{';
  print LATEXFILE &fix($subtitle).'} \hspace{4cm} \end{flushright}'."\n";
}

##############################################################################
## Main program
##############################################################################

$home=$ENV{'HOME'};
$psviewer="gv -landscape";
$config="";
$updateonly = "no";
## Parse command line
foreach $_ (@ARGV) {
  $psviewer = $1 if (/^--*psviewer\s*=\s*\"*\s*([^\"]*)\s*$/i);
  $config = $1 if (/^--*config\s*=\s*(.*)$/i);
  $updateonly = "yes" if (/^--*update$/i);
  &usage if (/^--*help$/i);
  &version if (/^--*version$/i);
}
&usage if ($config eq "");
$config =~ s/\~/$home/;		# ~ -> $HOME

## Default option values, in order of use below
$cdn="";
$lay="~/.tkcdlayout/layouts";
$Title="";
$rules="no";
$cddir="";
$maxdepth=0;
$showdirs="no";
$showfiles="no";
$exclude="";
$page="front";
$front_layout_type="taf";
$back_layout_type="btas";
$subtitle="";

## Parse config file
open (CONFIG, $config) || die "File not found : ".$config;
while (<CONFIG>) {
  unless (/^\#/ || /^\s*$/) 	# Skip comments and empty lines
    {
      s/\~/$home/;		# ~ -> $HOME
      eval "\$".$_;		# Set the variable. ex: $page=front
    }
}
close (CONFIG);

## Create LaTeX file
die "cdn option not set in ".$config if ($cdn eq "");
$cdn = $lay . "/" . $cdn;

open (LATEXFILE, ">$cdn.latex");

&BeginLaTeX($Title, $rules);
@getfiles_args = ($cddir, $maxdepth, $showdirs, $showfiles, $exclude);
&DoLayout($page, $front_layout_type, $back_layout_type, $subtitle, @getfiles_args);
print LATEXFILE "}\n";		# ends \landscape
print LATEXFILE '\end{document}' . "\n";
close (LATEXFILE);

## Process LaTeX file and view result
chdir $lay or die "Can't cd to $lay : $!";
system ("latex $cdn.latex") == 0 or die "latex command failed";
system ("dvips $cdn.dvi -o") == 0 or die "dvips command failed";
if ($updateonly eq "no") {
  print STDERR "*** Running $psviewer $cdn".".ps ***\n";
  $SIG{CHLD} = sub { wait };	# Avoid zombies
  unless (fork)			# Run the psviewer in a fork().
    {
      exec "$psviewer $cdn".".ps";
      die "$psviewer failed";
    }
}				# otherwise, just update .ps. User will have to press "Update" in gv.
exit 0;