Changeset 489011
- Timestamp:
- 01/12/2012 09:21:40 PM (14 years ago)
- Location:
- latex-everything/trunk
- Files:
-
- 1 added
- 2 edited
-
article-to-latex.php (modified) (13 diffs)
-
default-template.php (added)
-
html2latex.pl (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
latex-everything/trunk/article-to-latex.php
r489010 r489011 10 10 */ 11 11 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 15 13 * Probably want to create a rewrite rule so that the pdf attachment is at 16 14 * "/pdf" under the post itself. Use rewrite API … … 18 16 */ 19 17 20 /* function make_pdf_from_post ($ID) { 21 22 } */ 18 define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); // Plugin directory with trailing slash 19 define( 'HTML_TO_LATEX_SCRIPT', PLUGIN_DIR . 'html2latex.pl' ); 20 define( 'PDFLATEX', '/usr/texbin/pdflatex' ); 21 23 22 24 23 class Divinenephron_Article_To_Latex { 25 24 26 var $plugin_dir;27 var $html_to_latex_script;28 29 25 var $post; 30 26 31 var $html_file;32 27 var $latex_file; 33 28 var $pdf_file; … … 38 33 39 34 function __construct ( $post_id ) { 40 $this->plugin_dir = plugin_dir_path(__FILE__);41 $this->html_to_latex_script = $this->plugin_dir . 'html2latex.pl';42 43 35 $this->post = get_post( $post_id ); 44 36 } … … 46 38 function __destruct () { 47 39 // Unlink temporary files 48 unlink( $this->html_file );49 40 unlink( $this->latex_file ); 50 41 unlink( $this->pdf_file ); … … 54 45 55 46 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(); 63 49 if ( is_wp_error( $latex_result ) ) { 64 50 $this->record_error( $latex_result ); … … 79 65 return; 80 66 } 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 88 82 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' ); 94 90 fclose($f); 95 91 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 error107 $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';112 92 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. 113 102 } 114 103 … … 116 105 $tmp_file = tempnam( '/tmp', 'atl'); // Falls back on system temp dir 117 106 $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); 119 108 120 109 exec( $cmd, $latex_output, $v ); … … 127 116 $this->pdf_file = $dir . '/' . basename($this->latex_file, '.tex') . '.pdf'; 128 117 return $this->pdf_file; 129 130 118 } 131 119 132 120 function attach_pdf_file () { 133 $filename = 'a2l-post ie-' . $this->post->ID . '.pdf';121 $filename = 'a2l-post-' . $this->post->ID . '.pdf'; 134 122 135 123 $wp_filetype = wp_check_filetype( '.pdf' ); … … 173 161 } 174 162 163 175 164 /* Adds a2l_error to the redirect query when the error is first encounterd. 176 165 * After the redirect it prints the errors to the top of the admin page. … … 193 182 */ 194 183 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() ); 196 185 } 197 186 } … … 201 190 function a2l_save_post ( $post_id ) { 202 191 if ( get_post_type( $post_id ) == 'post' && !wp_is_post_revision( $post_id ) ) { 192 global $a2l; 203 193 $a2l = new Divinenephron_Article_To_Latex( $post_id ); 204 194 $a2l->create_pdf(); … … 215 205 if ( isset( $_GET['a2l_error'] ) ) { 216 206 $post_id = $_GET[ 'a2l_error' ]; 207 global $a2l; 217 208 $a2l = new Divinenephron_Article_To_Latex( $post_id ); 218 209 $a2l->create_pdf(); … … 221 212 add_action( 'admin_head', 'a2l_show_errors' ); 222 213 214 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 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 4 4 use HTML::Latex; 5 5 6 # Create an HTML2Latex parser 6 # Create an HTML2Latex parser. 7 7 my $parser = new HTML::Latex(); 8 8 9 9 # 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); 10 my $html_filename = @ARGV[0]; 11 open HTML, "<", $html_filename or die $!; 12 my $html = do { local( $/ ); <HTML> } ; 13 14 # Convert html to tex (without a preamble). 15 my $tex = $parser->parse_string($html); 16 17 print $tex; 18
Note: See TracChangeset
for help on using the changeset viewer.