Changeset 489019
- Timestamp:
- 01/12/2012 09:24:22 PM (14 years ago)
- Location:
- latex-everything/trunk
- Files:
-
- 1 added
- 3 edited
-
article-to-latex.php (modified) (10 diffs)
-
default-template.php (modified) (2 diffs)
-
html-to-latex.php (modified) (2 diffs)
-
uninstall.php (added)
Legend:
- Unmodified
- Added
- Removed
-
latex-everything/trunk/article-to-latex.php
r489018 r489019 10 10 */ 11 11 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 API15 * (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 17 17 18 18 define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); // Plugin directory with trailing slash … … 32 32 var $error; 33 33 34 var $unwanted_filter_functions = Array( 'wptexturize', 'convert_chars', 'esc_html', 'ent2ncr', '_wp_specialchars', 'sanitize_text_field', 'capital_P_dangit' ); 35 var $removed_filters; 34 36 35 37 function __construct ( $post_id ) { … … 39 41 function __destruct () { 40 42 // Unlink temporary files 43 /* 41 44 unlink( $this->latex_file ); 42 45 unlink( $this->pdf_file ); 43 46 unlink( $this->latex_file . '.aux' ); 44 47 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'); 45 67 } 46 68 … … 69 91 function make_latex_file () { 70 92 71 $template = $this-> get_latex_template();93 $template = $this->_get_latex_template(); 72 94 73 95 // Render the template 74 96 query_posts( 'p=' . $this->post->ID ); 97 $this->_set_up_latex_filters(); 75 98 ob_start(); 76 99 include( $template ); 77 100 $latex = ob_get_clean(); 101 $this->_undo_latex_filters(); 78 102 wp_reset_query(); 79 103 … … 102 126 } 103 127 104 function get_latex_template () {128 function _get_latex_template () { 105 129 $template = get_query_template('latex'); 106 130 if ( empty( $template) ) { … … 108 132 } 109 133 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; 110 161 } 111 162 … … 169 220 return WP_Error( 'wp_insert_attachment', 'Could not attach generated pdf' ); 170 221 } 222 add_post_meta( $attach_id, '_a2l_is_latex', 1, true ); 171 223 return $attach_id; 172 224 } … … 197 249 } 198 250 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 */ 253 function a2l_update_post ( $post_id ) { 202 254 if ( get_post_type( $post_id ) == 'post' && !wp_is_post_revision( $post_id ) ) { 203 255 global $a2l; … … 206 258 } 207 259 } 208 add_action('save_post', 'a2l_save_post'); 260 add_action('save_post', 'a2l_update_post'); 261 add_action('a2l_activation_update_post', 'a2l_update_post'); 209 262 210 263 /* If a2l_error is in the query, a previous post save created an error … … 223 276 add_action( 'admin_head', 'a2l_show_errors' ); 224 277 225 226 278 register_activation_hook( __FILE__, Array( 'A2l_Article_To_Latex', 'activate' ) ); 279 register_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 */ 286 function 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 */ 294 function 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 */ 306 function 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 */ 318 function 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 8 8 <?php the_post() ?> 9 9 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() ) ?>} 13 13 14 14 \begin{document} … … 16 16 \maketitle 17 17 18 <?php h tml_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() ) ) ?> 19 19 20 20 \end{document} -
latex-everything/trunk/html-to-latex.php
r489018 r489019 287 287 } 288 288 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 310 290 } 311 291 312 292 // API functions. 293 function h2l ( $html ) { 294 html_to_latex( $html ); 295 } 313 296 function html_to_latex ( $html ) { 314 297 echo get_html_to_latex( $html ); … … 321 304 } 322 305 306 // Filters. 307 // Run on html text nodes before output 308 function 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 315 function 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 325 function float_filter ( $latex ) { 326 return "\\begin{figure}[htbp]\n{$latex}\n\\end{figure}\n"; 327 } 328 323 329 // 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);330 add_filter('a2l_img_element', 'float_filter', 98); 331 add_filter('a2l_table_element', 'float_filter', 98); 332 add_filter('a2l_text', 'urlify_filter', 98); 333 add_filter('a2l_text','quote_expansion_filter', 99); 328 334 329 335 ?> 330
Note: See TracChangeset
for help on using the changeset viewer.