Plugin Directory

Changeset 489019


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

Added an install/uninstall procedure. Removed filters that interfered
with Latex rendering.

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

Legend:

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

    r489018 r489019  
    1010*/
    1111
    12  /* http://codex.wordpress.org/Function_Reference#Post.2C_Custom_Post_Type.2C_Page.2C_Attachment_and_Bookmarks_Functions
    13  * Probably want to create a rewrite rule so that the pdf attachment is at
    14  * "/pdf" under the post itself. Use rewrite API
    15  * (http://codex.wordpress.org/Rewrite_API)
    16  */
     12// TODO: Make documentation of API and install process.
     13// TODO: Extend generation to object other than posts - pages? categories? You'd just need to extend the
     14//       templating and the "get latex stuff" API. You might also have to use WP-Cron more (more PDF
     15//       being written at once), and hence improve the error handling to actually save error messages.
     16        
    1717
    1818define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) );            // Plugin directory with trailing slash
     
    3232    var $error;
    3333
     34    var $unwanted_filter_functions = Array( 'wptexturize', 'convert_chars', 'esc_html', 'ent2ncr', '_wp_specialchars', 'sanitize_text_field', 'capital_P_dangit' );
     35    var $removed_filters;
    3436
    3537    function __construct ( $post_id ) {
     
    3941    function __destruct () {
    4042        // Unlink temporary files
     43        /*
    4144        unlink( $this->latex_file );
    4245        unlink( $this->pdf_file );
    4346        unlink( $this->latex_file . '.aux' );
    4447        unlink( $this->latex_file . '.log' );
     48        */
     49    }
     50
     51    function activate () {
     52        // Create cron-jobs to re-create the pdf for every post.
     53        $args = Array( 'post-type' => 'post',
     54                      'numberposts' => -1,
     55                      'orderby' => 'post_date',
     56                      'order' => 'DESC',
     57                      'post_status' => null,
     58                    );
     59        $all_posts = get_posts( $args );
     60        foreach ( $all_posts as $post )
     61            wp_schedule_single_event( time(), 'a2l_activation_update_post', Array( $post->ID ) );
     62    }
     63
     64    function deactivate () {
     65        // Remove all cron jobs.
     66        wp_clear_scheduled_hook('a2l_activation_update_post');
    4567    }
    4668
     
    6991    function make_latex_file () {
    7092
    71         $template = $this->get_latex_template();
     93        $template = $this->_get_latex_template();
    7294
    7395        // Render the template       
    7496        query_posts( 'p=' . $this->post->ID );
     97        $this->_set_up_latex_filters();
    7598        ob_start();
    7699        include( $template );
    77100        $latex = ob_get_clean();
     101        $this->_undo_latex_filters();
    78102        wp_reset_query();
    79103
     
    102126    }
    103127
    104     function get_latex_template () {
     128    function _get_latex_template () {
    105129        $template = get_query_template('latex');
    106130        if ( empty( $template) ) {
     
    108132        }
    109133        return $template;
     134    }
     135
     136    /* Removes filters that interfere with Latex while processing Latex templates.
     137     * Records which filters were removed so that they can be added again later.
     138     */
     139    function _set_up_latex_filters() {
     140        global $wp_filter;
     141        foreach(         $wp_filter  as $tag      => $priorities ) {
     142            foreach(     $priorities as $priority => $filters ) {
     143                foreach( $filters    as $name     => $filter ) {
     144                    if ( in_array( $filter['function'], $this->unwanted_filter_functions ) ) {
     145                        $this->removed_filters[$tag][$priority][$name] = $filter;
     146                        unset( $wp_filter[$tag][$priority][$name] );
     147                    }
     148                }
     149            }
     150        }
     151        return;
     152    }
     153   
     154    /* Add the filters back in again once Latex template processing has finished.
     155     */
     156    function _undo_latex_filters() {
     157        global $wp_filter;
     158        // (Doesn't quite get priorities right)
     159        $wp_filter = array_merge_recursive( $wp_filter, $this->removed_filters );
     160        return;
    110161    }
    111162   
     
    169220            return WP_Error( 'wp_insert_attachment', 'Could not attach generated pdf' );
    170221        }
     222        add_post_meta( $attach_id, '_a2l_is_latex', 1, true );
    171223        return $attach_id;
    172224    }
     
    197249}
    198250
    199 /* Every time a post is saved run the converter.
    200  */
    201 function a2l_save_post ( $post_id ) {
     251/* Update the post. Run when a post is saved, and when the plugin is activated.
     252 */
     253function a2l_update_post ( $post_id ) {
    202254    if ( get_post_type( $post_id ) == 'post' && !wp_is_post_revision( $post_id ) ) {
    203255        global $a2l;
     
    206258    }
    207259}
    208 add_action('save_post', 'a2l_save_post');
     260add_action('save_post', 'a2l_update_post');
     261add_action('a2l_activation_update_post', 'a2l_update_post');
    209262
    210263/* If a2l_error is in the query, a previous post save created an error
     
    223276add_action( 'admin_head', 'a2l_show_errors' );
    224277
    225 
    226 
     278register_activation_hook( __FILE__, Array( 'A2l_Article_To_Latex', 'activate' ) );
     279register_deactivation_hook( __FILE__, Array( 'A2l_Article_To_Latex', 'deactivate' ) );
     280
     281/* API */
     282
     283/* Display the permalink to the Latex attachment page for the current post.
     284 * (not a direct link to the pdf, use get_latex_url() for that).
     285 */
     286function the_latex_permalink() {
     287        echo apply_filters('the_latex_permalink', get_latex_permalink());
     288}
     289
     290
     291/* Get the permalink for the current post or a post ID (not a direct link
     292 * to the pdf, use get_latex_url() for that).
     293 */
     294function get_latex_permalink($id = 0) {
     295    $attachment = get_latex_attachment( $post->ID );
     296
     297    if( !$attachment )
     298        return '';
     299   
     300    return get_attachment_link($attachment->ID);
     301}
     302
     303/* Returns a direct link to the Latex pdf for the current post or a post
     304 * ID.
     305 */
     306function get_latex_url( $id = 0 ) {
     307    $attachment = get_latex_attachment( $id );
     308
     309    if( !$attachment )
     310        return false;
     311
     312    return wp_get_attachment_url( $attachment->ID );
     313}
     314
     315/* Returns the latex attachment for the current post or a
     316 * post ID. Return false if this doesn't have one.
     317 */
     318function get_latex_attachment( $id=0 ) {
     319    if ( is_object($id) ) {
     320            $post = $id;
     321    } else {
     322            $post = &get_post($id);
     323    }
     324    if ( empty($post->ID) )
     325            return false;
     326
     327    $args = array( 'post_type' => 'attachment',
     328                   'numberposts' => 1,
     329                   'post_parent' => $post->ID,
     330                   'meta_key' => '_a2l_is_latex',
     331                   'meta_value' => 1,
     332                   );
     333    $attachments = get_posts($args);
     334   
     335    if (!$attachments)
     336        return false;
     337
     338    return $attachments[0];
     339}
     340?>
  • latex-everything/trunk/default-template.php

    r489013 r489019  
    88<?php the_post() ?>
    99
    10 \date{<?php the_date() ?>}
    11 \title{<?php the_title() ?>}
    12 \author{<?php the_author() ?>}
     10\date{<?php h2l( get_the_date() ) ?>}
     11\title{<?php h2l( get_the_title() ) ?>}
     12\author{<?php h2l( get_the_author() ) ?>}
    1313
    1414\begin{document}
     
    1616\maketitle
    1717
    18 <?php html_to_latex( apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))) ?>
     18<?php h2l( apply_filters( 'the_content', get_the_content() ) ) ?>
    1919
    2020\end{document}
  • latex-everything/trunk/html-to-latex.php

    r489018 r489019  
    287287    }
    288288
    289     // Run on html text nodes before output
    290     function quote_expansion_filter ( $text ) {
    291         $text = preg_replace( '/([^\s\[\{\)~])"/', "$1''", $text );
    292         $text = preg_replace( '/"/', '``', $text );
    293         return $text;
    294     }
    295 
    296     // Run on html text nodes before output
    297     function urlify_filter ( $text ) {
    298         // Wraps urls in \url{}
    299         // Lovingly stolen from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
    300         $pattern = '/(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/';
    301         return preg_replace( $pattern,
    302                 '\url{$0}',
    303                 $text );
    304     }
    305 
    306     // Run on <img> and <table> nodes before output
    307     function float_filter ( $latex, $element ) {
    308         return "\\begin{figure}[htbp]\n{$latex}\n\\end{figure}\n";
    309     }
     289
    310290}
    311291
    312292// API functions.
     293function h2l ( $html ) {
     294    html_to_latex( $html );
     295}
    313296function html_to_latex ( $html ) {
    314297    echo get_html_to_latex( $html );
     
    321304}
    322305
     306// Filters.
     307// Run on html text nodes before output
     308function quote_expansion_filter ( $text ) {
     309    $text = preg_replace( '/([^\s\[\{\)~])"/', "$1''", $text );
     310    $text = preg_replace( '/"/', '``', $text );
     311    return $text;
     312}
     313
     314// Run on html text nodes before output
     315function urlify_filter ( $text ) {
     316    // Wraps urls in \url{}
     317    // Lovingly stolen from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
     318    $pattern = '/(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/';
     319    return preg_replace( $pattern,
     320            '\url{$0}',
     321            $text );
     322}
     323
     324// Run on <img> and <table> nodes before output
     325function float_filter ( $latex ) {
     326    return "\\begin{figure}[htbp]\n{$latex}\n\\end{figure}\n";
     327}
     328
    323329// Register filters
    324 add_filter('a2l_img_element', array('A2l_Html_To_Latex', 'float_filter'), 98);
    325 add_filter('a2l_table_element', array('A2l_Html_To_Latex', 'float_filter'), 98);
    326 add_filter('a2l_text', array('A2l_Html_To_Latex', 'urlify_filter'), 98);
    327 add_filter('a2l_text', array('A2l_Html_To_Latex', 'quote_expansion_filter'), 99);
     330add_filter('a2l_img_element', 'float_filter', 98);
     331add_filter('a2l_table_element', 'float_filter', 98);
     332add_filter('a2l_text', 'urlify_filter', 98);
     333add_filter('a2l_text','quote_expansion_filter', 99);
    328334
    329335?>
    330 
Note: See TracChangeset for help on using the changeset viewer.