Commit a7dda9e12a10d5fbd445f363ba196418c463057a

Authored by Geoffrey Challen
1 parent 8a48710c

Done.

Makefile
1   -START = noxxxnote nodraft noblue
  1 +START = noxxxnote nodraft blue
2 2 END = missing
  3 +PYTEX = $(shell pwd)/pytex/
3 4 CLASS = $(PYTEX)/cls/sig-alternate.cls
4 5  
5 6 all: paper ABSTRACT
... ...
pytex/.gitignore 0 → 100644
  1 +/.pydevproject
  2 +/.project
  3 +*.pyc
  4 +*.swp
... ...
pytex/README 0 → 100644
  1 +To use, create a PYTEX environment variable and point it at the directory
  2 +where you install these files.
  3 +
  4 +This contains a combined Python-Latex build system useful for generating
  5 +scientific documents.
  6 +
  7 +bin/ contains useful Python tools for generating and building Latex
  8 +documents.
  9 +
  10 +cls/ contains useful class files for NSF proposals, letters, etc. Most of
  11 +these are based on the Memoir Latex package.
  12 +
  13 +make/ contains a Make system designed to be used with these Python tools.
  14 +
  15 +skel/ contains a set of example directories using each of the class files in
  16 +cls/.
  17 +
  18 +docs/ contains the Memoir Latex package and a reminder about how to write NSF
  19 +broader impact sections.
... ...
pytex/bin/.gitignore 0 → 100644
  1 +/flatex
  2 +*.pyc
... ...
pytex/bin/blank 0 → 100755
  1 +#!/usr/bin/env python
  2 +
  3 +import sys,subprocess
  4 +subprocess.check_output("convert -size %dx%d -density 300 -format pdf xc:white -bordercolor black -border 1x1 %s" % \
  5 + ((int(float(sys.argv[2]) * 300.) - 1),
  6 + (int(float(sys.argv[3]) * 300.) - 1),
  7 + sys.argv[1]), shell=True)
... ...
pytex/bin/clean 0 → 100755
  1 +#!/usr/bin/env python
  2 +
  3 +import lib, sys
  4 +from optparse import OptionParser
  5 +import re
  6 +
  7 +parser = OptionParser()
  8 +(options, args) = parser.parse_args()
  9 +
  10 +if len(args) < 2:
  11 + sys.exit(1)
  12 +
  13 +if args[0] == "-":
  14 + dirty_string = sys.stdin.read()
  15 +else:
  16 + dirty_string = open(args[0], "r").read()
  17 +
  18 +match = re.search(r"""(?ms)<clean:start>\s*(?P<excerpt>.*?)\s*<clean:end>""", dirty_string)
  19 +if match != None:
  20 + dirty_string = match.group('excerpt')
  21 +
  22 +if args[1] == "-":
  23 + outfile = sys.stdout
  24 +else:
  25 + outfile = open(args[1], "w")
  26 +
  27 +outfile.write(lib.clean(dirty_string).encode('utf8'))
... ...
pytex/bin/flatex.c 0 → 100644
  1 +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  2 + * flatex.c -
  3 + * Flatten a latex file into a single file, by explicitly including
  4 + * the files inclued by \include and \input commands. Also, if bibtex is
  5 + * beeing used, then includes the .bbl file into the resulting file. Thus,
  6 + * creating a stand alone latex file that can be emailed to someone else.
  7 + *
  8 + * Compile : gcc -o flatex flatex.c
  9 + * Tested on : Linux + gcc
  10 + * By : Sariel Har-Peled
  11 + * Email : sariel@math.tau.ac.il
  12 + * WEB Page : http://www.math.tau.ac.il/~sariel/flatex.html
  13 + * Status : You can do whatever you like with this program. please
  14 + * email me bugs & suggestions.
  15 + *
  16 + * To do : Add support to the includeonly command.
  17 + *-----------------------------------------------------------------------
  18 + * FLATEX 1.21, 1994, 1996, by Sariel Har-Peled.
  19 + *
  20 + * flatex - create a single latex file with no include/inputs
  21 + *
  22 + * flatex [-v] [-x FileName] [files]
  23 + * -v Verbose, display file structure.
  24 + * -x Unflatex: extract files from archive
  25 + * -q Quiet mode. Cleaner output but -x can not be used.
  26 + * -b Do not insert bibiliography file(.bbl)
  27 + *
  28 + * Flatex page: http://www.math.tau.ac.il/~sariel/flatex.html
  29 + *-----------------------------------------------------------------------
  30 + * History:
  31 + * 26/8/96, 1.21
  32 + * Fixed bug with includegraphics command.
  33 +\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  34 +
  35 +#include <stdlib.h>
  36 +#include <stdio.h>
  37 +#include <string.h>
  38 +#include <math.h>
  39 +#include <ctype.h>
  40 +
  41 +
  42 +/*======================================================================
  43 + * Static constants.
  44 +\*======================================================================*/
  45 +#define LINE_SIZE 1000
  46 +#define FALSE 0
  47 +#define TRUE 1
  48 +#define USE_ARGUMENT( X ) ((void)X)
  49 +
  50 +
  51 +/*======================================================================
  52 + * Types
  53 +\*======================================================================*/
  54 +typedef struct {
  55 + char verbose;
  56 + char fBibInsert, fQuiet;
  57 + int cSpecialInputLevel;
  58 + char szFullName[ LINE_SIZE ];
  59 +} structFlags;
  60 +
  61 +
  62 +/*======================================================================
  63 + * Static prototypes.
  64 +\*======================================================================*/
  65 +static void flatIt( FILE * flOut,
  66 + char * szInName,
  67 + int level,
  68 + structFlags * pFlags );
  69 +static void replaceExt( char * str, char * ext );
  70 +
  71 +
  72 +/*======================================================================
  73 + * Start of Code
  74 +\*======================================================================*/
  75 +
  76 +
  77 +static void spacesByLevel( int level )
  78 +{
  79 + while ( level > 0 ) {
  80 + printf( " " );
  81 + level--;
  82 + }
  83 +}
  84 +
  85 +
  86 +static void printHelp( void )
  87 +{
  88 + printf( "flatex - create a single latex file with no include/inputs\n" );
  89 + printf( "\n\tflatex [-v] [-x FileName] [files]\n" );
  90 + printf( "\t\t-v\tVerbose, display file structure.\n" );
  91 + printf( "\t\t-x\tUnflatex: extract files from archive\n" );
  92 + printf( "\t\t-q\tQuiet mode. Cleaner output but -x can not be used.\n" );
  93 + printf( "\t\t-b\tDo not insert bibiliography file(.bbl)\n" );
  94 + printf( "\nFlatex page: http://www.math.tau.ac.il/~sariel/flatex.html\n" );
  95 + printf( "\n" );
  96 +}
  97 +
  98 +
  99 +static void * myMalloc( unsigned int size )
  100 +{
  101 + void * ptr;
  102 +
  103 + ptr = malloc( size );
  104 + if ( ptr == NULL ) {
  105 + fprintf( stderr, "Not enough memory" );
  106 + exit( -1 );
  107 + }
  108 +
  109 + return ptr;
  110 +}
  111 +
  112 +
  113 +static void handleIncludeCommand( char * line,
  114 + char * lpszInclude,
  115 + FILE * flOut,
  116 + int level,
  117 + structFlags * pFlags )
  118 +{
  119 + char * lpszBrace, * lpszName, * lpszEndBrace;
  120 + char ch, fInput = 0;
  121 +
  122 + lpszBrace = NULL;
  123 +
  124 + if ( strncmp( lpszInclude, "\\input", 6 ) == 0 ) {
  125 + lpszBrace = lpszInclude + 6;
  126 + fInput = 1;
  127 + } else
  128 + if ( strncmp( lpszInclude, "\\include", 8 ) == 0 ) {
  129 + lpszBrace = lpszInclude + 8;
  130 + }
  131 +
  132 + ch = *lpszInclude;
  133 + *lpszInclude = 0;
  134 + fputs( line, flOut );
  135 + *lpszInclude = ch;
  136 +
  137 + lpszEndBrace = strchr( lpszBrace, '}' );
  138 + if ( *lpszBrace != '{' || lpszEndBrace == NULL ) {
  139 + fprintf( stderr, "ERROR: Expected brace not found.\n\n\tline:%s\n",
  140 + line );
  141 + exit( -1 );
  142 + }
  143 +
  144 + *lpszEndBrace = 0;
  145 + lpszName = (char *)myMalloc( LINE_SIZE );
  146 + strcpy( lpszName, lpszBrace + 1 );
  147 + if ( ! fInput )
  148 + replaceExt( lpszName, ".tex" );
  149 +
  150 + flatIt( flOut, lpszName, level + 1, pFlags );
  151 +
  152 + lpszEndBrace++;
  153 + while ( *lpszEndBrace ) {
  154 + *line++ = *lpszEndBrace++;
  155 + }
  156 + *line = 0;
  157 +
  158 + free( lpszName );
  159 +}
  160 +
  161 +
  162 +static char isBefore( char * lpszA, char * lpszB )
  163 +{
  164 + if ( lpszB == NULL )
  165 + return TRUE;
  166 + if ( lpszA == NULL )
  167 + return FALSE;
  168 +
  169 + if ( (int)( lpszA -lpszB ) < 0 ) {
  170 + return TRUE;
  171 + }
  172 + return FALSE;
  173 +}
  174 +
  175 +
  176 +static FILE * fopenTex( char * file,
  177 + char * mode )
  178 +{
  179 + FILE * fl;
  180 +
  181 + fl = fopen( file, mode );
  182 + if ( fl != NULL )
  183 + return fl;
  184 +
  185 + replaceExt( file, ".tex" );
  186 + fl = fopen( file, mode );
  187 +
  188 + return fl;
  189 +}
  190 +
  191 +
  192 +static char isTexFileExists( char * file )
  193 +{
  194 + FILE * fl;
  195 +
  196 + fl = fopenTex( file, "rt" );
  197 + if ( fl != NULL ) {
  198 + fclose( fl );
  199 + return 1;
  200 + }
  201 +
  202 + return 0;
  203 +}
  204 +
  205 +
  206 +static void addTexExt( char * file )
  207 +{
  208 + FILE * fl;
  209 +
  210 + fl = fopenTex( file, "rt");
  211 + if ( fl != NULL )
  212 + fclose( fl );
  213 +}
  214 +
  215 +
  216 +static char is_str_prefix( char * str, char * prefix )
  217 +{
  218 + int len;
  219 +
  220 + if ( str == NULL || prefix == NULL )
  221 + return 0;
  222 +
  223 + len = strlen( prefix );
  224 +
  225 + return (strncmp( str, prefix, len ) == 0);
  226 +}
  227 +
  228 +
  229 +static void flatIt( FILE * flOut,
  230 + char * pSzInName,
  231 + int level,
  232 + structFlags * pFlags )
  233 +{
  234 + FILE * flIn;
  235 + char * str, * lpszInput, * lpszInclude, * line, * lpszRem, *inc;
  236 + char * lpszLine, * lpszRemark, * lpszBib, * lpszBibStyle;
  237 + char * lpszNewCommand, * lpszName;
  238 + char cont;
  239 + char repFlag;
  240 + char szInName[ 100 ];
  241 + char fInclude;
  242 +
  243 + strcpy( szInName, pSzInName );
  244 +
  245 + addTexExt( szInName );
  246 + if ( ! pFlags->fQuiet )
  247 + fprintf( flOut, "%%%cflatex input: [%s]\n",
  248 + pFlags->cSpecialInputLevel > 0? '*' : ' ',
  249 + szInName );
  250 + if ( pFlags->verbose ) {
  251 + printf( "\t" );
  252 + spacesByLevel( level );
  253 + printf( "%s\n", szInName );
  254 + }
  255 +
  256 + line = (char *)myMalloc( LINE_SIZE );
  257 + lpszLine = (char *)myMalloc( LINE_SIZE );
  258 + lpszRemark = (char *)myMalloc( LINE_SIZE );
  259 +
  260 + flIn = fopenTex( szInName, "rt" );
  261 + if ( flIn == NULL ) {
  262 + fprintf( stderr, "Unable to open file: %s\n", szInName );
  263 + exit( -1 );
  264 + }
  265 +
  266 + *lpszRemark = 0;
  267 + while ( ! feof( flIn ) ) {
  268 + str = fgets( line, LINE_SIZE, flIn );
  269 + if ( str == NULL )
  270 + break;
  271 +
  272 + fInclude = FALSE;
  273 +
  274 + strcpy( lpszLine, line );
  275 +
  276 + lpszRem = strchr( line, '%' );
  277 + if ( lpszRem != NULL ) {
  278 + strcpy( lpszRemark, lpszRem );
  279 + *lpszRem = 0;
  280 + }
  281 +
  282 + do {
  283 + cont = 0;
  284 + lpszInput = strstr( line, "\\input" );
  285 +
  286 + lpszBib = strstr( line, "\\bibliography" );
  287 + lpszBibStyle = strstr( line, "\\bibliographystyle" );
  288 +
  289 + if ( pFlags->fBibInsert &&
  290 + ( lpszBib != NULL || lpszBibStyle != NULL ) ) {
  291 + lpszName = (char *)myMalloc( LINE_SIZE );
  292 +
  293 + strcpy( lpszName, lpszLine );
  294 + strcpy( lpszLine, pFlags->fQuiet? "%" : "%FLATEX-REM:" );
  295 + strcat( lpszLine, lpszName );
  296 +
  297 + if ( lpszBibStyle != NULL ) {
  298 + strcpy( lpszName, pFlags->szFullName );
  299 + replaceExt( lpszName, ".bbl" );
  300 +
  301 + pFlags->cSpecialInputLevel++;
  302 + flatIt( flOut, lpszName, level + 1, pFlags );
  303 + pFlags->cSpecialInputLevel--;
  304 +
  305 + if ( pFlags->verbose ) {
  306 + printf( "\t" );
  307 + spacesByLevel( level + 1 );
  308 + printf( "(Bibiliography)\n" );
  309 + }
  310 + }
  311 + break;
  312 + }
  313 +
  314 + inc = line;
  315 + do {
  316 + repFlag = 0;
  317 + lpszInclude = strstr( inc, "\\include" );
  318 +
  319 + if ( is_str_prefix( lpszInclude, "\\includeversion" )
  320 + || is_str_prefix( lpszInclude,
  321 + "\\includegraphics" ) ) {
  322 + repFlag = 1;
  323 + inc = lpszInclude + 1;
  324 + continue;
  325 + }
  326 +
  327 + if ( is_str_prefix( lpszInclude, "\\includeonly" ) ) {
  328 + fprintf( stderr, "WARNING: \"\\includeonly\" command "
  329 + "ignored\n" );
  330 + inc = lpszInclude + 1;
  331 + repFlag = 1;
  332 + continue;
  333 + }
  334 + if ( lpszInclude != NULL && isalpha( lpszInclude[ 8 ] ) ) {
  335 + fprintf( stderr,
  336 + "\nWarning: include-like(?) command ignored"
  337 + " at line:\n\t%s", lpszLine );
  338 + inc = lpszInclude + 1;
  339 + repFlag = 1;
  340 + continue;
  341 + }
  342 + } while ( repFlag );
  343 +
  344 + if ( isBefore( lpszInput, lpszInclude ) )
  345 + lpszInclude = lpszInput;
  346 +
  347 + if ( lpszInclude != NULL ) {
  348 + lpszNewCommand = strstr( line, "\\newcommand" );
  349 + if ( lpszNewCommand == NULL ) {
  350 + handleIncludeCommand( line, lpszInclude, flOut, level,
  351 + pFlags );
  352 + cont = 1;
  353 + fInclude = TRUE;
  354 + }
  355 + }
  356 + } while ( cont );
  357 + if ( fInclude ) {
  358 + strcat( line, lpszRemark );
  359 + fputs( line, flOut );
  360 + } else
  361 + fputs( lpszLine, flOut );
  362 + }
  363 +
  364 + fclose( flIn );
  365 + fputs( "\n", flOut );
  366 +
  367 + if ( ! pFlags->fQuiet )
  368 + fprintf( flOut, "%% flatex input end: [%s]\n", szInName );
  369 +
  370 + free( line );
  371 + free( lpszLine );
  372 + free( lpszRemark );
  373 +}
  374 +
  375 +
  376 +static void replaceExt( char * str, char * ext )
  377 +{
  378 + int len, ind;
  379 +
  380 + len = strlen( str );
  381 + ind = len - 1;
  382 + while ( ind >= 0 && str[ ind ] != '.' && str[ ind ] != '\\' &&
  383 + str[ ind ] != '/' )
  384 + ind--;
  385 +
  386 + if ( ind >= 0 && str[ ind ] == '.' ) {
  387 + str[ ind ] = 0;
  388 + }
  389 +
  390 + strcat( str, ext );
  391 +}
  392 +
  393 +static char strCmpPrefixAndCopy( char * line,
  394 + char * str,
  395 + char * outName )
  396 +{
  397 + char * pos, * pPreLine;
  398 +
  399 + pPreLine = line;
  400 +
  401 + pos = strstr( line, str );
  402 + if ( pos == NULL )
  403 + return 0;
  404 +
  405 + line = pos + strlen( str );
  406 + strcpy( outName, line );
  407 + pos = strchr( outName, ']' );
  408 +
  409 + if ( pos == NULL ) {
  410 + fprintf( stderr, "Error encountered in line: [%s]", pPreLine );
  411 + exit( -1 );
  412 + }
  413 + *pos = 0;
  414 +
  415 + return 1;
  416 +}
  417 +
  418 +
  419 +static void writeFile( FILE * flIn,
  420 + char * pOutName,
  421 + int level )
  422 +{
  423 + FILE * flOut;
  424 + char * lpszLine;
  425 + char line[ LINE_SIZE ], outName[ LINE_SIZE ];
  426 + char flag;
  427 +
  428 + outName[ 0 ] = 0;
  429 +
  430 + if ( pOutName == NULL ) {
  431 + flOut = NULL;
  432 + printf( "Scanning for flatex archive start...\n" );
  433 + } else {
  434 + flOut = fopen( pOutName, "wt" );
  435 + if ( flOut == NULL ) {
  436 + fprintf( stderr, "Unable to open file: %s", pOutName );
  437 + exit( -1 );
  438 + }
  439 + spacesByLevel( level );
  440 + printf( "[%s]\n", pOutName );
  441 + }
  442 +
  443 + do {
  444 + lpszLine = fgets( line, LINE_SIZE, flIn );
  445 + if ( lpszLine == NULL )
  446 + break;
  447 +
  448 + flag = strCmpPrefixAndCopy( line, "% flatex input end: [", outName );
  449 + if ( flag ) {
  450 + if ( flOut == NULL ) {
  451 + fprintf( stderr, "Something is wrong!!!!\n" );
  452 + exit( -1 );
  453 + }
  454 + //spacesByLevel( level );
  455 + // printf( "/\n" );
  456 + //printf( "Writing [%s] done\n", outName );
  457 + break;
  458 + }
  459 +
  460 + flag = strCmpPrefixAndCopy( line, "% flatex input: [", outName );
  461 + if ( flag ) {
  462 + writeFile( flIn, outName, level + 1 );
  463 + if ( flOut != NULL )
  464 + fprintf( flOut, "\\input{%s}\n", outName );
  465 + } else {
  466 + flag = strCmpPrefixAndCopy( line, "%*flatex input: [", outName );
  467 + if ( flag ) {
  468 + writeFile( flIn, outName, level + 1 );
  469 + } else {
  470 + if ( flOut != NULL ) {
  471 + if ( strncmp( line, "%FLATEX-REM:", 12 ) == 0 )
  472 + fputs( line + 12, flOut );
  473 + else
  474 + fputs( line, flOut );
  475 + }
  476 + }
  477 + }
  478 + } while ( ! feof( flIn ) );
  479 +
  480 + if ( flOut != NULL )
  481 + fclose( flOut );
  482 +}
  483 +
  484 +
  485 +static void flatOutFile( char * fileName,
  486 + structFlags * pFlags )
  487 +{
  488 + FILE * flIn;
  489 +
  490 + USE_ARGUMENT( pFlags );
  491 +
  492 + flIn = fopen( fileName, "rt" );
  493 + if ( flIn == NULL ) {
  494 + fprintf( stderr, "Unable to open file: %s", fileName );
  495 + exit( -1 );
  496 + }
  497 +
  498 + writeFile( flIn, NULL, 0 );
  499 +
  500 + fclose( flIn );
  501 +}
  502 +
  503 +
  504 +static void flatFile( char * fileName,
  505 + structFlags * pFlags )
  506 +{
  507 + char * szInName, * szOutName;
  508 + int inLen;
  509 + FILE * flOut;
  510 +
  511 + szInName = (char *)myMalloc( LINE_SIZE );
  512 + szOutName = (char *)myMalloc( LINE_SIZE );
  513 +
  514 + strcpy( szInName, fileName );
  515 + if ( ! isTexFileExists( szInName ) ) {
  516 + fprintf( stderr, "--Unable to open file: [%s]\n", fileName );
  517 + exit( -1 );
  518 + }
  519 +
  520 + inLen = strlen( szInName );
  521 + if ( inLen < 4 || ( szInName[ inLen ] != '.' &&
  522 + strcmp( szInName + inLen - 4, ".tex" ) != 0 ) ) {
  523 + strcat( szInName, ".tex" );
  524 + }
  525 +
  526 + printf( "input file: [%s]\n", szInName );
  527 +
  528 + strcpy( pFlags->szFullName, szInName );
  529 +
  530 + strcpy( szOutName, szInName );
  531 + replaceExt( szOutName, ".flt" );
  532 +
  533 + flOut = fopen( szOutName, "wt" );
  534 + if ( flOut == NULL ) {
  535 + fprintf( stderr, "Unable to open file: %s", szOutName );
  536 + exit( -1 );
  537 + }
  538 +
  539 + flatIt( flOut, szInName, 0, pFlags );
  540 +
  541 + fclose( flOut );
  542 +
  543 + printf( "\n\tFile: \"%s\" generated\n", szOutName );
  544 +}
  545 +
  546 +
  547 +static char isFlag( char * str, char ch )
  548 +{
  549 + if ( str[ 0 ] == '-' &&
  550 + ( str[ 1 ] == ch || str[ 1 ] == toupper( ch ) )
  551 + && ( str[ 2 ] == 0 ) )
  552 + return TRUE;
  553 +
  554 + return FALSE;
  555 +}
  556 +
  557 +
  558 +int main( int argc, char * argv[] )
  559 +{
  560 + int ind;
  561 + structFlags sFlags;
  562 +
  563 + printf( "FLATEX 1.21, 1994, 1996, by Sariel Har-Peled.\n\n" );
  564 + if ( argc == 1 )
  565 + printHelp();
  566 +
  567 + sFlags.verbose = FALSE;
  568 + sFlags.fBibInsert = TRUE;
  569 + sFlags.cSpecialInputLevel = 0;
  570 + *sFlags.szFullName = 0;
  571 + sFlags.fQuiet = FALSE;
  572 +
  573 + for ( ind = 1; ind < argc; ind++ ) {
  574 + if ( isFlag( argv[ ind ], 'v' ) ) {
  575 + sFlags.verbose = TRUE;
  576 + continue;
  577 + }
  578 + if ( isFlag( argv[ ind ], 'b' ) ) {
  579 + sFlags.fBibInsert = FALSE;
  580 + continue;
  581 + }
  582 + if ( isFlag( argv[ ind ], 'q' ) ) {
  583 + sFlags.fQuiet = TRUE;
  584 + continue;
  585 + }
  586 + if ( isFlag( argv[ ind ], 'x' ) ) {
  587 + flatOutFile( argv[ ind + 1 ], &sFlags );
  588 + ind++;
  589 + continue;
  590 + }
  591 +
  592 + flatFile( argv[ ind ], &sFlags );
  593 + }
  594 + return 0;
  595 +}
  596 +
  597 +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  598 + *
  599 + * flatex.c - End of File
  600 +\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  601 +
... ...
pytex/bin/lib.py 0 → 100644
  1 +import re
  2 +
  3 +def clean(inlines):
  4 +
  5 + removecomments = re.compile(r"^(%.*)$", re.M)
  6 + inlines = removecomments.sub("", inlines)
  7 + fixpercents = re.compile(r"\\%", re.M)
  8 + inlines = fixpercents.sub("%", inlines)
  9 + removetex = re.compile(r"~?\\(((sub)*)section(\*?)|cite|chapter|thispagestyle)\*+\{([^\}]+)\}", re.M)
  10 + inlines = removetex.sub("", inlines)
  11 + removetex2 = re.compile(r"\\(clearpage)", re.M)
  12 + inlines = removetex2.sub("", inlines)
  13 + keeptex = re.compile(r"\\(textit|textbf|texttt|textsc|sloppypar)\{([^\}]+)\}", re.M)
  14 + while True:
  15 + beforelines = inlines
  16 + inlines = keeptex.sub(r"\2", inlines)
  17 + if inlines == beforelines:
  18 + break
  19 + keeptex2 = re.compile(r"\{\\scshape\s+([^\}]+)\}", re.S | re.M)
  20 + inlines = keeptex2.sub(r"\1", inlines)
  21 + quotes = re.compile(r"(``|'')", re.M)
  22 + inlines = quotes.sub(r'"', inlines)
  23 + phonelab_macro = re.compile(r"\\PhoneLab{}", re.M)
  24 + inlines = phonelab_macro.sub("PhoneLab", inlines)
  25 + sciwinet_macro = re.compile(r"\\SciWiNet{}", re.M)
  26 + inlines = sciwinet_macro.sub("SciWiNet", inlines)
  27 + composite_macro = re.compile(r"\\ComPoSiTe{}", re.M)
  28 + inlines = composite_macro.sub("ComPoSiTe", inlines)
  29 + agiledroid_macro = re.compile(r"\\AG{}", re.M)
  30 + inlines = agiledroid_macro.sub("AgileDroid", inlines)
  31 + wifi_macro = re.compile(r"\\wifi{}", re.M)
  32 + inlines = wifi_macro.sub("Wifi", inlines)
  33 + keep_together = re.compile(r"~", re.M)
  34 + inlines = keep_together.sub(" ", inlines)
  35 + en_dashes = re.compile(r"([^-])--([^-])", re.M)
  36 + inlines = en_dashes.sub(u"\\1\u2013\\2", inlines)
  37 + em_dashes = re.compile(r"([^-])---([^-])", re.M)
  38 + inlines = em_dashes.sub(u"\\1\u2014\\2", inlines)
  39 + enum = re.compile(r"\\begin\{enumerate\}(.*?)\\end\{enumerate\}", re.S | re.M)
  40 +
  41 + class Counter:
  42 + def __init__(self):
  43 + self.count = 0
  44 + def reset(self):
  45 + self.count = 0
  46 + def increment(self, matchObject):
  47 + self.count += 1
  48 + return str(self.count) + "."
  49 +
  50 + def match(m):
  51 + c = Counter()
  52 + item = re.compile(r"\\item")
  53 + text = item.sub(c.increment, m.group(1))
  54 + c.reset()
  55 + return text
  56 + inlines = enum.sub(match, inlines)
  57 +
  58 + removeitem = re.compile(r"~?\\item", re.M)
  59 + inlines = removeitem.sub("", inlines)
  60 + removeflushenumbf = re.compile(r"\\begin\{flushenumbf\}\s+(.*?)\s+\\end\{flushenumbf\}", re.S | re.M)
  61 + inlines = removeflushenumbf.sub(r"\1", inlines)
  62 + removebeginabstract = re.compile(r"\\begin\{abstract\}\s+(.*?)\s+\\end\{abstract\}", re.S | re.M)
  63 + inlines = removebeginabstract.sub(r"\1", inlines)
  64 +
  65 + lines = re.split(r'\s{2,}', inlines)
  66 +
  67 + while re.match(lines[0], r"^\s*$"):
  68 + lines = lines[1:]
  69 + if len(lines) == 0:
  70 + return ""
  71 + while re.match(lines[-1], r"^\s*$"):
  72 + lines = lines[:-1]
  73 + if len(lines) == 0:
  74 + return ""
  75 +
  76 + output = '\n\n'.join([re.sub(r'\n', ' ', line) for line in lines])
  77 + return output
... ...
pytex/bin/number 0 → 100755
  1 +#!/usr/bin/env python
  2 +
  3 +from optparse import OptionParser
  4 +import sys, subprocess, time, re, shlex, tempfile, os
  5 +
  6 +parser = OptionParser()
  7 +parser.add_option("-s", "--skip", dest="skip", type=int, default=0, help="number of initial pages to skip (default 0)")
  8 +parser.add_option("-a", "--avoid", dest="avoid", type=str, default="", help="pages to avoid, comma separated (default \"\")")
  9 +(options, args) = parser.parse_args()
  10 +
  11 +avoid = options.avoid.split(",")
  12 +avoid = [int(a) for a in avoid]
  13 +
  14 +infile = args[0]
  15 +outfile = args[1]
  16 +ininfo = subprocess.Popen("pdfinfo \"%s\"" % (infile), shell=True, stdout=subprocess.PIPE).communicate()[0]
  17 +origpages = int(re.search(r'Pages:\s+(\d+)', ininfo).group(1))
  18 +numpages = origpages - options.skip
  19 +
  20 +latexstart = r'''\documentclass[11pt]{memoir}
  21 +\usepackage{times}
  22 +\maxdeadcycles=1000
  23 +\setstocksize{11in}{8.5in}
  24 +\settrimmedsize{11in}{8.5in}{*}
  25 +\settrims{0pt}{0pt}
  26 +\setlrmarginsandblock{1in}{1in}{*}
  27 +\setulmarginsandblock{1in}{1in}{*}
  28 +\setheadfoot{0.1pt}{36pt}
  29 +\setmarginnotes{0.5cm}{1.5cm}{0.1cm}
  30 +\checkandfixthelayout
  31 +\copypagestyle{number}{headings}
  32 +\makeoddhead{number}{}{}{}
  33 +\makeevenhead{number}{}{}{}
  34 +\makeoddfoot{number}{}{\thepage}{}
  35 +\makeevenfoot{number}{}{\thepage}{}
  36 +\begin{document}
  37 +\pagestyle{number}'''
  38 +
  39 +latexend = r'''\end{document}'''
  40 +
  41 +startdir = os.getcwd()
  42 +tempdir = tempfile.mkdtemp()
  43 +subprocess.call("cp \"%s\" \"%s\"/A.pdf" % (infile, tempdir), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  44 +os.chdir(tempdir)
  45 +latexfile = open('B.tex', 'w')
  46 +print >>latexfile, latexstart
  47 +for a in range(numpages):
  48 + print >>latexfile, r'''\mbox{}
  49 +\newpage'''
  50 +print >>latexfile, latexend
  51 +latexfile.close()
  52 +subprocess.Popen("pdflatex --interaction=nonstopmode B.tex", shell=True, stdout=subprocess.PIPE).communicate()[0]
  53 +subprocess.Popen(r"pdftk A.pdf burst output A%03d.pdf", shell=True, stdout=subprocess.PIPE).communicate()[0]
  54 +subprocess.Popen(r"pdftk B.pdf burst output B%03d.pdf", shell=True, stdout=subprocess.PIPE).communicate()[0]
  55 +Boffset = options.skip
  56 +for Aindex in range(origpages):
  57 + Aindex += 1
  58 + if (Aindex <= Boffset) or ((Aindex - Boffset) in avoid):
  59 + subprocess.Popen(r"cp A%03d.pdf C%03d.pdf" % (Aindex, Aindex), shell=True, stdout=subprocess.PIPE).communicate()[0]
  60 + else:
  61 + subprocess.Popen(r"pdftk A%03d.pdf background B%03d.pdf output C%03d.pdf" % (Aindex, Aindex - Boffset, Aindex), shell=True, stdout=subprocess.PIPE).communicate()[0]
  62 +subprocess.Popen(r"pdftk %s output D.pdf" % (' '.join(["C%03d.pdf" % (i + 1) for i in range(origpages)])), shell=True, stdout=subprocess.PIPE).communicate()[0]
  63 +subprocess.call("cp D.pdf \"%s\"/\"%s\"" % (startdir, outfile), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  64 +
  65 +os.chdir(startdir)
... ...
pytex/bin/rotateandstitch 0 → 100755
  1 +#!/usr/bin/env python
  2 +
  3 +from optparse import OptionParser
  4 +import sys, subprocess, time, re, shlex
  5 +
  6 +parser = OptionParser()
  7 +(options, args) = parser.parse_args()
  8 +
  9 +pagesize = re.compile(r'Page size:\s+(\d+)\s+x\s+(\d+)')
  10 +files = []
  11 +outpdf = args.pop(0)
  12 +for arg in args:
  13 + output = subprocess.Popen("pdfinfo \"%s\"" % (arg), shell=True, stdout=subprocess.PIPE).communicate()[0]
  14 + pagematch = pagesize.search(output)
  15 + if pagematch == None:
  16 + continue
  17 + if pagematch.group(1) < pagematch.group(2):
  18 + files.append(arg)
  19 + continue
  20 + infile = arg
  21 + outfile = re.sub(r'.pdf', '.rotate.pdf', infile)
  22 + output = subprocess.Popen("pdftk \"%s\" cat 1-endW output \"%s\"" % (infile, outfile), shell=True, stdout=subprocess.PIPE).communicate()[0]
  23 + files.append(outfile)
  24 +
  25 +def order(pdf):
  26 + nummatch = re.search(r'(\d+)', pdf)
  27 + if nummatch == None:
  28 + return 0
  29 + else:
  30 + return int(nummatch.group(1))
  31 +
  32 +files = sorted(files, key=order)
  33 +output = subprocess.Popen("pdftk %s cat output \"%s\"" % (' '.join(['"%s"' % (file) for file in files]), outpdf), shell=True, stdout=subprocess.PIPE).communicate()[0]
... ...
pytex/bin/texincludes 0 → 100755
  1 +#!/usr/bin/env python
  2 +
  3 +import sys,re,glob,StringIO,os,tempfile,filecmp,shutil
  4 +from optparse import OptionParser
  5 +
  6 +parser = OptionParser()
  7 +(options, args) = parser.parse_args()
  8 +
  9 +if len(args) < 1:
  10 + sys.exit(1)
  11 +
  12 +files = glob.glob("*.tex")
  13 +
  14 +if len(files) == 0:
  15 + sys.exit(0)
  16 +
  17 +outfile = tempfile.NamedTemporaryFile(delete=False)
  18 +
  19 +docfile = re.compile(r"""(?m)^(?!\s*%).*\\begin\{document\}""")
  20 +inputs = re.compile(r"""(?m)^(?!\s*%).*\\input{(.*)}""")
  21 +bibs = re.compile(r"""(?m)^(?!\s*%).*\\bibliography\{(.*)\}""")
  22 +citations = re.compile(r"""^(?m)^(?!\s*%).*\\(?:no)?cite""")
  23 +graphics = re.compile(r"""(?m)^(?!\s*%).*\\includegraphics(\[.*?\])?\{(.*?)\}""")
  24 +withpdf = re.compile(r"^.*\.pdf$")
  25 +nobibtex = re.compile(r"""(?m)^% !NOBIBTEX!""")
  26 +
  27 +nobibtexs = {}
  28 +
  29 +output = StringIO.StringIO()
  30 +allnames = []
  31 +
  32 +for f in files:
  33 + lines = open(f, "r").read()
  34 + if not docfile.search(lines):
  35 + continue
  36 + input_files = []
  37 + bib_files = []
  38 + graphic_files = []
  39 + toprocess = [f]
  40 +
  41 + docitations = False
  42 + dontbibtex = False
  43 + fbasename = os.path.splitext(f)[0]
  44 +
  45 + while len(toprocess) > 0:
  46 + try:
  47 + lines = open(toprocess[0], "r").read()
  48 + if nobibtex.search(lines):
  49 + nobibtexs[toprocess[0]] = True
  50 + else:
  51 + nobibtexs[toprocess[0]] = False
  52 + if len(citations.findall(lines)) > 0:
  53 + docitations = True
  54 + toprocess += inputs.findall(lines)
  55 + b = bibs.finditer(lines)
  56 + for m in b:
  57 + allbibs = m.group(1).split(",")
  58 + for bib in allbibs:
  59 + bib_files.append(bib + ".bib")
  60 + g = graphics.finditer(lines)
  61 + for m in g:
  62 + if withpdf.match(m.group(2)):
  63 + graphic_files.append(m.group(2))
  64 + else:
  65 + path, ext = os.splitext(m.group(2))
  66 + if ext == '':
  67 + graphic_files.append(path + ".pdf")
  68 + else:
  69 + graphic_files.append(m.group(2))
  70 + except:
  71 + True
  72 + input_files.append(toprocess.pop(0))
  73 +
  74 + all_files = input_files
  75 + all_files.extend(graphic_files)
  76 + all_files.extend(bib_files)
  77 + for file in args[1:]:
  78 + all_files.append(file)
  79 + allnames.append(fbasename)
  80 +
  81 + print >>output, "%s : LOG := %s.log" % (fbasename, fbasename)
  82 + print >>output, "%s : PDF := %s.pdf" % (fbasename, fbasename)
  83 + print >>output, "%s : $(START) %s.pdf $(END)" % (fbasename, fbasename)
  84 + print >>output, "%s.ps : %s.pdf" % (fbasename, fbasename)
  85 + print >>output, "%s.pdf %s.blg : .deps %s" % (fbasename, fbasename, " ".join(all_files))
  86 + if docitations and not nobibtexs[f]:
  87 + print >>output, "\tpdflatex -shell-escape %s" % (f)
  88 + print >>output, "\tbibtex %s" % (fbasename)
  89 + print >>output, "\tpdflatex -shell-escape %s" % (f)
  90 + print >>output, "\tpdflatex -shell-escape %s" % (f)
  91 + else:
  92 + print >>output, "\tpdflatex -shell-escape %s" % (f)
  93 + print >>output, "\tpdflatex -shell-escape %s" % (f)
  94 + tex_files = [all_file for all_file in all_files if all_file.endswith(".tex")]
  95 + print >>output, "spell-%s : %s" % (fbasename, " ".join(tex_files),)
  96 + print >>output, "\tispell %s" % (" ".join(tex_files),)
  97 +
  98 +print >>outfile, output.getvalue(),
  99 +print >>outfile, "PDFS = %s" % (" ".join([n + ".pdf" for n in allnames]))
  100 +outfile.close()
  101 +
  102 +if not os.path.exists(args[0]) or not filecmp.cmp(outfile.name, args[0], shallow=False):
  103 + shutil.move(outfile.name, args[0])
  104 +else:
  105 + os.unlink(outfile.name)
... ...
pytex/bin/wc 0 → 100755
  1 +#!/usr/bin/env python
  2 +
  3 +import lib
  4 +import sys, re, os
  5 +from optparse import OptionParser
  6 +
  7 +parser = OptionParser()
  8 +parser.add_option("-o", "--overonly", dest="overonly", action="store_true", default=False, help="only display sections over the word count (default False)")
  9 +(options, args) = parser.parse_args()
  10 +
  11 +if len(args) < 2:
  12 + sys.exit(1)
  13 +
  14 +if args[0] == "-":
  15 + inlines = sys.stdin.read()
  16 +else:
  17 + try:
  18 + inlines = open(args[0], "r").read()
  19 + except:
  20 + sys.exit(1)
  21 +
  22 +if args[1] == "-":
  23 + outfile = sys.stdout
  24 +else:
  25 + try:
  26 + outfile = open(args[1], "w")
  27 + except:
  28 + sys.exit(1)
  29 +
  30 +clean = re.compile(r'<wc:start description="([^"]*)" max=(\d+)>(.*?)<wc:end>', re.S)
  31 +index = 1
  32 +for f in clean.finditer(inlines):
  33 + description = f.group(1)
  34 + max = int(f.group(2))
  35 + count = len(lib.clean(f.group(3)).split())
  36 + if not options.overonly or count > max:
  37 + if count > max:
  38 + char = "*"
  39 + else:
  40 + char = " "
  41 + print "%c %2d. M:%3d C:%3d %s" % (char, index, max, count, description)
  42 + index += 1
  43 +clean = re.compile(r'<cc:start description="([^"]*)" max=(\d+)>(.*?)<cc:end>', re.S)
  44 +index = 1
  45 +for f in clean.finditer(inlines):
  46 + description = f.group(1)
  47 + max = int(f.group(2))
  48 + count = len(lib.clean(f.group(3)).strip())
  49 + if not options.overonly or count > max:
  50 + if count > max:
  51 + char = "*"
  52 + else:
  53 + char = " "
  54 + print "%c %2d. M:%3d C:%3d %s" % (char, index, max, count, description)
  55 + index += 1
... ...
pytex/make/Makerules 0 → 100644
  1 +SHELL := /bin/bash
  2 +export TEXINPUTS :=.:$(PYTEX)/cls:
  3 +
  4 +# 16 Nov 2010 : GWA : Watch all .tex files below this directory to determine
  5 +# when to rebuild the dependencies.
  6 +
  7 +TEXFILES = $(shell find . -name "*.tex")
  8 +
  9 +# 16 Nov 2010 : GWA : Kind of a nasty hack, but we use a special Python
  10 +# script to regenerate make rules which are then loaded by the
  11 +# include below. This was the least nasty way of getting
  12 +# complex Latex dependencies to rebuild properly, while also
  13 +# enabling/disabling Bibtex as needed.
  14 +
  15 +.deps: $(TEXFILES)
  16 + @$(PYTEX)/bin/texincludes .deps $(CLASS)
  17 +include .deps
  18 +
  19 +%.ps : %.pdf
  20 + acroread -toPostScript $<
  21 +
  22 +allclean: rulesclean
  23 + @/bin/rm -f .deps
  24 +
  25 +rulesclean:
  26 + @/bin/rm -f *.dvi *.aux *.ps *~ *.log *.lot *.lof *.toc *.blg *.bbl url.sty *.out *.bak $(PDFS)
  27 +
  28 +# 16 Nov 2010 : GWA : Special dummy targets below.
  29 +
  30 +xxxnote:
  31 + @echo "\\newcommand{\\XXXnote}[1]{\\textcolor{red}{\\bfseries XXX: #1}}" > .xxxnote-new
  32 + @if [ -n "`diff -N 2>/dev/null .xxxnote .xxxnote-new`" ]; then\
  33 + mv .xxxnote-new .xxxnote; \
  34 + else\
  35 + rm -f .xxxnote-new; \
  36 + fi
  37 +
  38 +noxxxnote:
  39 + @echo "\\newcommand{\\XXXnote}[1]{}" > .xxxnote-new
  40 + @if [ -n "`diff -N 2>/dev/null .xxxnote .xxxnote-new`" ]; then\
  41 + mv .xxxnote-new .xxxnote; \
  42 + else\
  43 + rm -f .xxxnote-new; \
  44 + fi
  45 +
  46 +draft:
  47 + @echo "\\def\\isdraft{1}" > .draft-new
  48 + @if [ -n "`diff -N 2>/dev/null .draft .draft-new`" ]; then\
  49 + mv .draft-new .draft; \
  50 + else\
  51 + rm -f .draft-new; \
  52 + fi
  53 +
  54 +nodraft:
  55 + @echo "" > .draft-new
  56 + @if [ -n "`diff -N 2>/dev/null .draft .draft-new`" ]; then\
  57 + mv .draft-new .draft; \
  58 + else\
  59 + rm -f .draft-new; \
  60 + fi
  61 +
  62 +blue:
  63 + @echo "\\def\\isblue{1}" > .blue-new
  64 + @if [ -n "`diff -N 2>/dev/null .blue .blue-new`" ]; then\
  65 + mv .blue-new .blue; \
  66 + else\
  67 + rm -f .blue-new; \
  68 + fi
  69 +
  70 +noblue:
  71 + @echo "" > .blue-new
  72 + @if [ -n "`diff -N 2>/dev/null .blue .blue-new`" ]; then\
  73 + mv .blue-new .blue; \
  74 + else\
  75 + rm -f .blue-new; \
  76 + fi
  77 +
  78 +.embed.pdf: $(PDF)
  79 + gs -dSAFER -dNOPLATFONTS -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true -sOutputFile=.embed.pdf -f $(PDF)
  80 + @cp .embed.pdf $(PDF)
  81 +
  82 +embed: .embed.pdf
  83 +
  84 +MISSINGREFERENCES = $(strip $(shell grep Ref $(LOG) | awk '{print substr($$4, 2, length($$4) - 2)}'))
  85 +MISSINGCITATIONS = $(strip $(shell grep Cit $(LOG) | awk '{print substr($$4, 2, length($$4) - 2)}'))
  86 +missing:
  87 + @if [ "$(MISSINGREFERENCES)" != "" ]; then\
  88 + echo "-------------------------------------------------------------";\
  89 + echo "Missing references:";\
  90 + echo "-------------------------------------------------------------";\
  91 + echo $(MISSINGREFERENCES);\
  92 + fi
  93 + @if [ "$(MISSINGCITATIONS)" != "" ]; then\
  94 + echo "-------------------------------------------------------------";\
  95 + echo "Missing citations:";\
  96 + echo "-------------------------------------------------------------";\
  97 + echo $(MISSINGCITATIONS);\
  98 + fi
  99 +
  100 +missing-fail: missing
  101 + @if [ "$(MISSINGREFERENCES)" != "" ]; then false; fi
  102 + @if [ "$(MISSINGCITATIONS)" != "" ]; then false; fi
  103 +
  104 +pages: $(PDF)
  105 + @pdfinfo $(PDF) 2>/dev/null | grep "Pages" | awk '{print "$(PDF)", $$2;}'
  106 +
  107 +# 16 Nov 2010 : GWA : Phony targets.
  108 +
  109 +.PHONY : pages rulesclean missing-fail missing xxxnote noxxxnote draft nodraft blue noblue clean allclean all figures wc
... ...