Plugin Directory

Changeset 489017


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

Added some filters and documentation to html-to-latex.php.

Location:
latex-everything/trunk
Files:
2 edited

Legend:

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

    r489013 r489017  
    3939    function __destruct () {
    4040        // Unlink temporary files
     41        /*
    4142        unlink( $this->latex_file );
    4243        unlink( $this->pdf_file );
    4344        unlink( $this->latex_file . '.aux' );
    4445        unlink( $this->latex_file . '.log' );
     46        */
    4547    }
    4648
  • latex-everything/trunk/html-to-latex.php

    r489016 r489017  
    11<?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 */
    235
    336class A2l_Html_To_Latex {
     
    4477                'ul'         => Array ( 'handler' => 'environment', 'tex' => 'itemize'          ),
    4578                );
     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        }
    4684        $this->tags = apply_filters( 'a2l_tags', $this->tags );
    4785    }
    4886
     87    /* Function to convert an html string into Latex. Used by the get_html_to_latex()
     88     * function.
     89     */
    4990    function html_to_latex ( $html_string ) {
    5091
     
    60101    }
    61102
     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     */
    62106    function _texify ( $parent_element ) {
    63107        $output = '';
     
    70114                    // If the tag has a handler, send it to the handler.
    71115                    $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 );
    75121                    } else {
    76122                        $output .= $this->_texify( $element );
     
    81127                }
    82128            } 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 );
    84132            } else if ( $element->nodeType == XML_DOCUMENT_NODE ) {
    85133                $output .= $this->_texify( $element );
     
    138186    }
    139187
     188    /* Creates a Latex table from a <table> element. */
    140189    function _create_latex_table( $table ) {
    141190        $output = '';
     
    175224        $output .= "\n\n\\begin{tabular}{{$column_alignments}}\n";
    176225        $output .= "\\hline\n";
    177        
     226
    178227        for( $r = 0; $r < $row_count; ++$r ) {
    179228            $row = $rows->item( $r );
     
    226275    }
    227276
     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     */
    228280    function _get_local_image( $src ) {
    229281        $path = str_replace( site_url(), ABSPATH, $src );
     
    254306
    255307// API functions.
    256 global $a2l_html_to_latex;
    257 $a2l_html_to_latex = new A2l_Html_To_Latex();
    258308function html_to_latex ( $html ) {
    259309    echo get_html_to_latex( $html );
     
    261311function get_html_to_latex ( $html ) {
    262312    global $a2l_html_to_latex;
     313    if ( empty ( $a2l_html_to_latex ) )
     314        $a2l_html_to_latex = new A2l_Html_To_Latex();
    263315    return $a2l_html_to_latex->html_to_latex( $html );
    264316}
Note: See TracChangeset for help on using the changeset viewer.