close

Plugin Directory

Changeset 489013


Ignore:
Timestamp:
01/12/2012 09:22:27 PM (14 years ago)
Author:
divinenephron
Message:

Re-wrote html2latex.pl in php, except for img and table handling.

Location:
latex-everything/trunk
Files:
1 added
2 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • latex-everything/trunk/article-to-latex.php

    r489012 r489013  
    44Plugin URI:
    55Version: 0.1
    6 Author: Devon Buchanan
    7 Author URI:
     6Author: Divinenephron (Devon Buchanan)
     7Author URI: http://divinenephron.co.uk
    88Description:
    99License: GPL
     
    1717
    1818define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) );            // Plugin directory with trailing slash
    19 define( 'HTML_TO_LATEX_SCRIPT', PLUGIN_DIR . 'html2latex.pl' );
    2019define( 'PDFLATEX', '/usr/texbin/pdflatex' );
    2120
    22 
    23 class Divinenephron_Article_To_Latex {
     21include('html-to-latex.php'); // Include functions to convert html to latex.
     22
     23
     24class A2l_Article_To_Latex {
    2425
    2526    var $post;
     
    8485
    8586        // Open and write the latex the temporary file.
    86     if ( !$f = @fopen( $this->latex_file, 'w' ) )
     87        if ( !WP_DEBUG )
     88            $f = @fopen( $this->latex_file, 'w' );
     89        else
     90            $f = fopen( $this->latex_file, 'w' );
     91    if ( !$f )
    8792        return new WP_Error( 'fopen', 'Could not open temporary file for writing' );
    88     if ( false === @fwrite($f, $latex) )
     93        if ( ! WP_DEBUG )
     94            $w = @fwrite($f, $latex);
     95        else
     96            $w = fwrite($f, $latex);
     97    if ( !$w )
    8998        return new WP_Error( 'fwrite', 'Could not write to temporary file' );
    9099    fclose($f);
     
    99108        }
    100109        return $template;
    101         // TODO: Add a default template in the plugin directory.
    102110    }
    103111   
     
    105113        $tmp_file = tempnam( '/tmp', 'atl'); // Falls back on system temp dir
    106114        $dir = dirname( $this->latex_file );
     115
     116        // Tell Latex to search in the theme directory for class/style files.
     117        putenv('TEXINPUTS=.:' . get_stylesheet_directory() . ':' . get_template_directory() . ':');
    107118        $cmd = sprintf( 'cd %s; %s --interaction=nonstopmode %s 2>&1', $dir, PDFLATEX, $this->latex_file);
    108119
     
    191202    if ( get_post_type( $post_id ) == 'post' && !wp_is_post_revision( $post_id ) ) {
    192203        global $a2l;
    193         $a2l = new Divinenephron_Article_To_Latex( $post_id );
     204        $a2l = new A2l_Article_To_Latex( $post_id );
    194205        $a2l->create_pdf();
    195206    }
     
    206217        $post_id = $_GET[ 'a2l_error' ];
    207218        global $a2l;
    208         $a2l = new Divinenephron_Article_To_Latex( $post_id );
     219        $a2l = new A2l_Article_To_Latex( $post_id );
    209220        $a2l->create_pdf();
    210221    }
     
    213224
    214225
    215 /* Helper functions that converts html (e.g. from the_content() ) into LaTeX.
    216  */
    217 function a2l_latex ( $html ) {
    218         $latex = a2l_get_latex( $html );
    219         if ( is_wp_error( $latex ) ) {
    220             print "Error: {$latex->get_error_message()}";
    221         } else {
    222             print $latex;
    223         }
    224 }
    225 
    226 function a2l_get_latex ( $html ) {
    227     // Get the name of a temporary file.
    228     if ( !$html_file = tempnam( sys_get_temp_dir(), 'a2l-' ) ) // Should fall back on system's temp dir if /tmp does not exist
    229         return new WP_Error( 'tempnam', 'Could not create temporary file.' );
    230     $dir = dirname( $html_file );
    231 
    232     // Open and write the html the temporary file.
    233     if ( !$f = @fopen( $html_file, 'w' ) )
    234         return new WP_Error( 'fopen', 'Could not open temporary file for writing' );
    235     if ( false === @fwrite($f, $html) )
    236         return new WP_Error( 'fwrite', 'Could not write to temporary file' );
    237     fclose($f);
    238 
    239     // Convert the temporary file to using html2latex.pl (the latex is put into its stdout)
    240     $cmd = sprintf( 'cd %s; perl %s %s 2>/dev/null', $dir, HTML_TO_LATEX_SCRIPT, $html_file );
    241     exec( $cmd, $h2l_output, $v );
    242     $latex = implode( "\n", $h2l_output );
    243     if ( $v != 0 ) { // There was an error
    244         return new WP_Error('html2latex.pl', $latex);
    245     }
    246 
    247     unlink( $html_file );
    248 
    249     return $latex;
    250 }
     226
  • latex-everything/trunk/default-template.php

    r489012 r489013  
    1616\maketitle
    1717
    18 <?php a2l_latex( apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))) ?>
     18<?php html_to_latex( apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))) ?>
    1919
    2020\end{document}
Note: See TracChangeset for help on using the changeset viewer.