Changeset 489017
- Timestamp:
- 01/12/2012 09:23:37 PM (14 years ago)
- Location:
- latex-everything/trunk
- Files:
-
- 2 edited
-
article-to-latex.php (modified) (1 diff)
-
html-to-latex.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
latex-everything/trunk/article-to-latex.php
r489013 r489017 39 39 function __destruct () { 40 40 // Unlink temporary files 41 /* 41 42 unlink( $this->latex_file ); 42 43 unlink( $this->pdf_file ); 43 44 unlink( $this->latex_file . '.aux' ); 44 45 unlink( $this->latex_file . '.log' ); 46 */ 45 47 } 46 48 -
latex-everything/trunk/html-to-latex.php
r489016 r489017 1 1 <?php 2 /* HTML-to-Latex 3 * ------------- 4 * Use the functions html_to_latex() and get_html_to_latex() to convert HTML strings to Latex. 5 * This has been mostly based on html2latex.pm by Peter Thatcher (http://html2latex.sourceforge.net/). 6 * 7 * Filters 8 * ------- 9 * a2l_tags The tags array used to determine which handler function alter the 10 * Latex output. It take the form. 11 * Array( '<tag_name>' => Array ( 'handler' => <callback>, 12 * 'tex' => <data for callback> ), 13 * '<tag_name>' => [...] 14 * ); 15 * Tags that aren't in the tags array are ignored, while their children 16 * and the text inside them are printed. 17 * To create your own handler, just add, or change an entry in this array 18 * so that it points to your callback. 19 * Arguments: 20 * $tags (The default array of tags) 21 * 22 * a2l_<name>_element Filters the latex generated for the specifed element just before 23 * it is added to the output for get_html_to_latex(). 24 * Arguments: 25 * $latex (The Latex generated from the element) 26 * $element (The DOMElement object the Latex was generated from) 27 * 28 * a2l_text Filters the text between HTML nodes just before it is added to the 29 * output for get_html_to_latex(). I use it to convert quote to latex 30 * style (``...'') and to detect urls. 31 * Arguments: 32 * $text (The text between HTML nodes) 33 * $element (The DOMText object the text is from) 34 */ 2 35 3 36 class A2l_Html_To_Latex { … … 44 77 'ul' => Array ( 'handler' => 'environment', 'tex' => 'itemize' ), 45 78 ); 79 // Convert handlers to the form [$this, _<type>_handler]. 80 foreach ( $this->tags as $tag => $value ) { 81 $handler = "_{$this->tags[$tag]['handler']}_handler"; 82 $this->tags[$tag]['handler'] = Array( $this, $handler ); 83 } 46 84 $this->tags = apply_filters( 'a2l_tags', $this->tags ); 47 85 } 48 86 87 /* Function to convert an html string into Latex. Used by the get_html_to_latex() 88 * function. 89 */ 49 90 function html_to_latex ( $html_string ) { 50 91 … … 60 101 } 61 102 103 /* Iterates over the children of the node. Elements have their handlers called, 104 * (which usually print something then call texify again) while text is printed. 105 */ 62 106 function _texify ( $parent_element ) { 63 107 $output = ''; … … 70 114 // If the tag has a handler, send it to the handler. 71 115 $tag = $this->tags[$element->tagName]; 72 $handler = "_{$tag['handler']}_handler"; 73 if ( method_exists( $this, $handler ) ) { 74 $output .= $this->$handler( $element, $tag['tex'] ); 116 $handler = $tag['handler']; 117 if ( is_callable( $handler ) ) { 118 $output .= apply_filters( "a2l_{$element->tagName}_element", 119 call_user_func( $handler, $element, $tag['tex'] ), 120 $element ); 75 121 } else { 76 122 $output .= $this->_texify( $element ); … … 81 127 } 82 128 } else if ( $element->nodeType == XML_TEXT_NODE ) { 83 $output .= apply_filters( 'a2l_text', $element->wholeText ); 129 $output .= apply_filters( 'a2l_text', 130 $element->wholeText, 131 $element ); 84 132 } else if ( $element->nodeType == XML_DOCUMENT_NODE ) { 85 133 $output .= $this->_texify( $element ); … … 138 186 } 139 187 188 /* Creates a Latex table from a <table> element. */ 140 189 function _create_latex_table( $table ) { 141 190 $output = ''; … … 175 224 $output .= "\n\n\\begin{tabular}{{$column_alignments}}\n"; 176 225 $output .= "\\hline\n"; 177 226 178 227 for( $r = 0; $r < $row_count; ++$r ) { 179 228 $row = $rows->item( $r ); … … 226 275 } 227 276 277 /* Translates a url pointing to a local file into the local file's path. 278 * If the file doesn't exist on the server, returns an empty string. 279 */ 228 280 function _get_local_image( $src ) { 229 281 $path = str_replace( site_url(), ABSPATH, $src ); … … 254 306 255 307 // API functions. 256 global $a2l_html_to_latex;257 $a2l_html_to_latex = new A2l_Html_To_Latex();258 308 function html_to_latex ( $html ) { 259 309 echo get_html_to_latex( $html ); … … 261 311 function get_html_to_latex ( $html ) { 262 312 global $a2l_html_to_latex; 313 if ( empty ( $a2l_html_to_latex ) ) 314 $a2l_html_to_latex = new A2l_Html_To_Latex(); 263 315 return $a2l_html_to_latex->html_to_latex( $html ); 264 316 }
Note: See TracChangeset
for help on using the changeset viewer.