Back to my blog Back to my projects

Dokuwiki ODT plugin

Changeset f19f41f


Ignore:
Timestamp:
08/11/10 18:15:28 (3 years ago)
Author:
Aurélien Bompard <aurelien@…>
Branches:
master
Children:
1015dd2
Parents:
71e6e54
git-author:
Aurélien Bompard <aurelien@…> (08/11/10 18:15:28)
git-committer:
Aurélien Bompard <aurelien@…> (08/11/10 18:15:28)
Message:

Basic syntax highlighting support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • renderer.php

    rbd5433f rf19f41f  
    3737    var $in_list_item = false; 
    3838    var $in_paragraph = false; 
     39    var $highlight_style_num = 1; 
    3940    // Automatic styles. Will always be added to content.xml and styles.xml 
    4041    var $autostyles = array( 
     
    195196            ); 
    196197 
     198        //$headers = array('Content-Type'=>'text/plain'); p_set_metadata($ID,array('format' => array('odt' => $headers) )); return ; // DEBUG 
    197199        // send the content type header, new method after 2007-06-26 (handles caching) 
    198200        $output_filename = str_replace(':','-',$ID).".odt"; 
     
    268270    function document_end(){ 
    269271        global $conf; 
     272        //$this->doc .= $this->_odtAutoStyles(); return; // DEBUG 
    270273        if ($this->template) { // template chosen 
    271274            if (file_exists($conf['mediadir'].'/'.$this->getConf("tpl_dir")."/".$this->template)) { //template found 
     
    833836 
    834837    function preformatted($text) { 
    835         $text = $this->_xmlEntities($text); 
     838        $this->_preformatted($text); 
     839    } 
     840 
     841    function file($text, $language=null, $filename=null) { 
     842        $this->_highlight($text, $language); 
     843    } 
     844 
     845    function quote_open() { 
     846        if (!$this->in_paragraph) { // only start a new par if we're not already in one 
     847            $this->p_open(); 
     848        } 
     849        $this->doc .= "&gt;"; 
     850    } 
     851 
     852    function quote_close() { 
     853        if ($this->in_paragraph) { // only close the paragraph if we're actually in one 
     854            $this->p_close(); 
     855        } 
     856    } 
     857 
     858    function code($text, $language=null, $filename=null) { 
     859        $this->_highlight($text, $language); 
     860    } 
     861 
     862    function _preformatted($text, $notescaped=true) { 
     863        if ($notescaped) { 
     864            $text = $this->_xmlEntities($text); 
     865        } 
    836866        if (strpos($text, "\n") !== FALSE and strpos($text, "\n") == 0) { 
    837867            // text starts with a newline, remove it 
     
    854884    } 
    855885 
    856     function file($text) { 
    857         $this->preformatted($text); 
    858     } 
    859  
    860     function quote_open() { 
    861         if (!$this->in_paragraph) { // only start a new par if we're not already in one 
    862             $this->p_open(); 
    863         } 
    864         $this->doc .= "&gt;"; 
    865     } 
    866  
    867     function quote_close() { 
    868         if ($this->in_paragraph) { // only close the paragraph if we're actually in one 
    869             $this->p_close(); 
    870         } 
    871     } 
    872  
    873     function code($text, $language = NULL) { 
    874         // FIXME we could convert GeSHi HTML and CSS classes to ODT XML here 
    875         $this->preformatted($text); 
     886    function _highlight($text, $language=null) { 
     887        global $conf; 
     888        if (is_null($language)) { 
     889            $this->_preformatted($text); 
     890            return; 
     891        } 
     892        // from inc/parserutils.php:p_xhtml_cached_geshi() 
     893        require_once(DOKU_INC . 'inc/geshi.php'); 
     894 
     895        $geshi = new GeSHi($text, $language, DOKU_INC . 'inc/geshi'); 
     896        $geshi->set_encoding('utf-8'); 
     897        // $geshi->enable_classes(); DO NOT WANT ! 
     898        $geshi->set_header_type(GESHI_HEADER_PRE); 
     899        $geshi->enable_keyword_links(false); 
     900 
     901        // remove GeSHi's wrapper element (we'll replace it with our own later) 
     902        // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 
     903        $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r"); 
     904        $highlighted_code = str_replace("</span>", "</text:span>", $highlighted_code); 
     905        $highlighted_code = preg_replace_callback('/<span style="([^"]+)">/', array('renderer_plugin_odt','_convert_css_styles'), $highlighted_code); 
     906        $highlighted_code = str_replace("&nbsp;", "&#xA0;", $highlighted_code); 
     907        $this->_preformatted($highlighted_code, false); 
     908    } 
     909 
     910    function _convert_css_styles($matches) { 
     911        $all_css_styles = $matches[1]; 
     912        // parse the CSS attribute 
     913        $css_styles = array(); 
     914        foreach(explode(";", $all_css_styles) as $css_style) { 
     915            $css_style_array = explode(":", $css_style); 
     916            if (!trim($css_style_array[0]) or !trim($css_style_array[1])) { 
     917                continue; 
     918            } 
     919            $css_styles[trim($css_style_array[0])] = trim($css_style_array[1]); 
     920        } 
     921        // create the ODT xml style 
     922        $style_name = "highlight." . $this->highlight_style_num; 
     923        $this->highlight_style_num += 1; 
     924        $style_content = ' 
     925            <style:style style:name="'.$style_name.'" style:family="text"> 
     926                <style:text-properties '; 
     927        foreach($css_styles as $style_key=>$style_value) { 
     928            // Hats off to those who thought out the OpenDocument spec: styling syntax is similar to CSS ! 
     929            $style_content .= 'fo:'.$style_key.'="'.$style_value.'" '; 
     930        } 
     931        $style_content .= '/> 
     932            </style:style>'; 
     933        // add the style to the library 
     934        $this->autostyles[$style_name] = $style_content; 
     935        // now make use of the new style 
     936        return '<text:span text:style-name="'.$style_name.'"><!-- '.$matches[1].' -->'; 
    876937    } 
    877938 
Note: See TracChangeset for help on using the changeset viewer.