Plugin Directory

Changeset 489011


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

Made the plugin read LaTeX from a PHP template.

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

Legend:

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

    r489010 r489011  
    1010*/
    1111
    12 /* Latex documents redered by html2latex.pl.
    13  * Documents saved using attachment API (see "Attachments" header)
    14  * http://codex.wordpress.org/Function_Reference#Post.2C_Custom_Post_Type.2C_Page.2C_Attachment_and_Bookmarks_Functions
     12 /* http://codex.wordpress.org/Function_Reference#Post.2C_Custom_Post_Type.2C_Page.2C_Attachment_and_Bookmarks_Functions
    1513 * Probably want to create a rewrite rule so that the pdf attachment is at
    1614 * "/pdf" under the post itself. Use rewrite API
     
    1816 */
    1917
    20 /* function make_pdf_from_post ($ID) {
    21    
    22 } */
     18define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) );            // Plugin directory with trailing slash
     19define( 'HTML_TO_LATEX_SCRIPT', PLUGIN_DIR . 'html2latex.pl' );
     20define( 'PDFLATEX', '/usr/texbin/pdflatex' );
     21
    2322
    2423class Divinenephron_Article_To_Latex {
    2524
    26     var $plugin_dir;
    27     var $html_to_latex_script;
    28 
    2925    var $post;
    3026
    31     var $html_file;
    3227    var $latex_file;
    3328    var $pdf_file;
     
    3833
    3934    function __construct ( $post_id ) {
    40         $this->plugin_dir = plugin_dir_path(__FILE__);
    41         $this->html_to_latex_script = $this->plugin_dir . 'html2latex.pl';
    42 
    4335        $this->post = get_post( $post_id );
    4436    }
     
    4638    function __destruct () {
    4739        // Unlink temporary files
    48         unlink( $this->html_file );
    4940        unlink( $this->latex_file );
    5041        unlink( $this->pdf_file );
     
    5445
    5546    function create_pdf() {
    56         $html_result = $this->make_html_file();
    57         if ( is_wp_error( $html_result ) ) {
    58             $this->record_error( $html_result );
    59             return;
    60         }
    61    
    62         $latex_result = $this->html_file_to_latex_file();
     47   
     48        $latex_result = $this->make_latex_file();
    6349        if ( is_wp_error( $latex_result ) ) {
    6450            $this->record_error( $latex_result );
     
    7965        return;
    8066    }
    81    
    82     function make_html_file () {
    83         $html = $this->post->post_content;
    84         $html = apply_filters('the_content', $html);
    85         $html = str_replace(']]>', ']]>', $html);
    86 
    87         if ( !$this->html_file = tempnam( sys_get_temp_dir(), 'a2l-' ) ) // Should fall back on system's temp dir if /tmp does not exist
     67
     68    function make_latex_file () {
     69
     70        $template = $this->get_latex_template();
     71
     72        // Render the template       
     73        query_posts( 'p=' . $this->post->ID );
     74        ob_start();
     75        include( $template );
     76        $latex = ob_get_clean();
     77        wp_reset_query();
     78
     79        // Get the name of a temporary file.
     80
     81        if ( !$this->latex_file = tempnam( sys_get_temp_dir(), 'a2l-' ) ) // Should fall back on system's temp dir if /tmp does not exist
    8882        return new WP_Error( 'tempnam', 'Could not create temporary file.' );
    89 
    90     if ( !$f = @fopen( $this->html_file, 'w' ) )
    91         return new WP_Error( 'fopen', 'Could not open TEX file for writing' );
    92     if ( false === @fwrite($f, $html) )
    93         return new WP_Error( 'fwrite', 'Could not write to TEX file' );
     83        $dir = dirname( $this->latex_file );
     84
     85        // Open and write the latex the temporary file.
     86    if ( !$f = @fopen( $this->latex_file, 'w' ) )
     87        return new WP_Error( 'fopen', 'Could not open temporary file for writing' );
     88    if ( false === @fwrite($f, $latex) )
     89        return new WP_Error( 'fwrite', 'Could not write to temporary file' );
    9490    fclose($f);
    9591
    96     return $this->html_file;
    97     }
    98    
    99     function html_file_to_latex_file () {
    100    
    101         $dir = dirname( $this->html_file );
    102         $cmd = sprintf( 'cd %s; perl %s %s 2>&1', $dir, $this->html_to_latex_script, $this->html_file );
    103 
    104         exec( $cmd, $h2l_output, $v );
    105 
    106         if ( $v != 0 ) { // There was an error
    107             $h2l_output = join("\n", $h2l_output);
    108             return new WP_Error('html2latex.pl', $h2l_output);
    109         }
    110 
    111         $this->latex_file = $dir . '/' . basename($this->html_file) . '.tex';
    11292        return $this->latex_file;
     93    }
     94
     95    function get_latex_template () {
     96        $template = get_query_template('latex');
     97        if ( empty( $template) ) {
     98            $template = PLUGIN_DIR . 'default-template.php';
     99        }
     100        return $template;
     101        // TODO: Add a default template in the plugin directory.
    113102    }
    114103   
     
    116105        $tmp_file = tempnam( '/tmp', 'atl'); // Falls back on system temp dir
    117106        $dir = dirname( $this->latex_file );
    118         $cmd = sprintf( 'cd %s; /usr/texbin/pdflatex --interaction=nonstopmode %s 2>&1', $dir, $this->latex_file);
     107        $cmd = sprintf( 'cd %s; %s --interaction=nonstopmode %s 2>&1', $dir, PDFLATEX, $this->latex_file);
    119108
    120109        exec( $cmd, $latex_output, $v );
     
    127116        $this->pdf_file = $dir . '/' . basename($this->latex_file, '.tex') . '.pdf';
    128117        return $this->pdf_file;
    129    
    130118    }
    131119
    132120    function attach_pdf_file () {
    133         $filename = 'a2l-postie-' . $this->post->ID . '.pdf';
     121        $filename = 'a2l-post-' . $this->post->ID . '.pdf';
    134122
    135123        $wp_filetype = wp_check_filetype( '.pdf' );
     
    173161    }
    174162
     163
    175164    /* Adds a2l_error to the redirect query when the error is first encounterd.
    176165     * After the redirect it prints the errors to the top of the admin page.
     
    193182     */
    194183    function display_error () {
    195         printf( "<div class=\"error\"><p>Article to Latex error</p><p>%s</p><p>%s</p></div>\n", $this->error->get_error_code(), $this->error->get_error_message() );
     184        printf( "<div class=\"error\"><p>Article to Latex error: %s</p><pre>%s</pre></div>\n", $this->error->get_error_code(), $this->error->get_error_message() );
    196185    }
    197186}
     
    201190function a2l_save_post ( $post_id ) {
    202191    if ( get_post_type( $post_id ) == 'post' && !wp_is_post_revision( $post_id ) ) {
     192        global $a2l;
    203193        $a2l = new Divinenephron_Article_To_Latex( $post_id );
    204194        $a2l->create_pdf();
     
    215205    if ( isset( $_GET['a2l_error'] ) ) {
    216206        $post_id = $_GET[ 'a2l_error' ];
     207        global $a2l;
    217208        $a2l = new Divinenephron_Article_To_Latex( $post_id );
    218209        $a2l->create_pdf();
     
    221212add_action( 'admin_head', 'a2l_show_errors' );
    222213
     214
     215/* Helper functions that converts html (e.g. from the_content() ) into LaTeX.
     216 */
     217function 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
     226function 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    print $html;
     232
     233    // Open and write the html the temporary file.
     234    if ( !$f = @fopen( $html_file, 'w' ) )
     235        return new WP_Error( 'fopen', 'Could not open temporary file for writing' );
     236    if ( false === @fwrite($f, $html) )
     237        return new WP_Error( 'fwrite', 'Could not write to temporary file' );
     238    fclose($f);
     239
     240    // Convert the temporary file to using html2latex.pl (the latex is put into its stdout)
     241    $cmd = sprintf( 'cd %s; perl %s %s 2>&1', $dir, HTML_TO_LATEX_SCRIPT, $html_file );
     242    exec( $cmd, $h2l_output, $v );
     243    $latex = implode( "\n", $h2l_output );
     244    if ( $v != 0 ) { // There was an error
     245        return new WP_Error('html2latex.pl', $latex);
     246    }
     247
     248    unlink( $html_file );
     249
     250    return $latex;
     251}
  • latex-everything/trunk/html2latex.pl

    r489009 r489011  
    44use HTML::Latex;
    55
    6 # Create an HTML2Latex parser
     6# Create an HTML2Latex parser.
    77my $parser = new HTML::Latex();
    88
    99# Read from the provided HTML file, and ouput to a tex file of the same name.
    10 my $html_filename = $tex_filename = @ARGV[0];
    11 $tex_filename =~ s/\.[^\/]+$/.tex/;
    12 $parser->html2latex($html_filename,$tex_filename);
     10my $html_filename = @ARGV[0];
     11open HTML, "<", $html_filename or die $!;
     12my $html = do { local( $/ ); <HTML> } ;
     13
     14# Convert html to tex (without a preamble).
     15my $tex = $parser->parse_string($html);
     16
     17print $tex;
     18
Note: See TracChangeset for help on using the changeset viewer.