Not logged in. · Lost password · Register
Forum: General Help and Support Development RSS
odt rendering, preformatted text and colors
Page:  1  2  next 
Avatar
schplurtz (Moderator) #1
Member since Nov 2009 · 493 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
Subject: odt rendering, preformatted text and colors
Hi,

I'm trying to add odt rendering to the cli syntax plugin. In the end I'll need to get the odt equivalent of this html snippet :
<pre><span style="color:red">prompt $</span><span style="color: green">command</span></pre>
There is a bit of documentation in https://www.dokuwiki.org/plugin:odt:implementodtsupport and I tried to understand the code in renderer/page.php, but I still don't know how to properly style preformatted text in this renderer.

First question. There are methods whose name start with an underscore. What does it mean ? are these methods special in any way ?

Second, I don't seem to be able to add colored spans whith renderer_plugin_odt_page->preformatted() nor with renderer_plugin_odt_page->_preformatted() and there is no method to open a preformatted paragraph. Did I miss something ?

So far, I can't use preformatted text, so I resort to using standard paragraph, with monospace font. But this collapses multiple spaces into one single space (just like html does with spaces), which is not good. So I also replace all pairs of spaces by <space><non-breakable-space>. spaces are visually preserved, but this is just a hack. I'm looking for something nicer that wouldn't modify the data.  Current code looks like this :
$renderer->_odtParagraphOpenUseProperties(array('font-family' => 'Bitstream Vera Sans Mono',....));
$renderer->_odtSpanOpenUseProperties(array('color' => 'red'));
$renderer->cdata(str_replace('  ', $sp_and_nbsp, $line[0]));
$renderer->_odtSpanClose();
$renderer->_odtSpanOpenUseProperties(array('color' => 'green'));
$renderer->cdata(str_replace('  ', $sp_and_nbsp, $line[1]));
$renderer->_odtSpanClose();
etc...

Any idea how I could do this correctly ?
Avatar
Michaelsy #2
Member since Jun 2015 · 969 posts · Location: Düsseldorf, Germany
Group memberships: Members
Show profile · Link to this post
Quote by schplurtz:
First question. There are methods whose name start with an underscore. What does it mean ? are these methods special in any way ?
That seems to answer this question: https://www.php.net/manual/en/language.oop5.magic.php

BTW: Magic functions start with two underscores.
By Patreon.com a few eurons can be fed into the code phasers of
the DokuWiki engine. Besides, Andi's posts are worth reading.
Avatar
schplurtz (Moderator) #3
Member since Nov 2009 · 493 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
Hi Michaelsy,
I know you want to help, but please don't post noise when you don't have the answer. Magic functions have nothing to do with my question.
Avatar
Michaelsy #4
Member since Jun 2015 · 969 posts · Location: Düsseldorf, Germany
Group memberships: Members
Show profile · Link to this post
Quote by schplurtz:
... but please don't post noise when you don't have the answer.

Fewer words would have sufficed to point out my mistake.
By Patreon.com a few eurons can be fed into the code phasers of
the DokuWiki engine. Besides, Andi's posts are worth reading.
This post was edited on 2019-07-16, 07:32 by Michaelsy.
Avatar
LarsDW223 #5
Member since Sep 2014 · 447 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
odt rendering, preformatted text and colors

Yes, I think exactly that is not possible at the moment. You can create preformatted text as you wrote but there is no way to insert colored text spans.

But I created a test branch for you in the ODT plugin called "schplurtz". Please install it and then try something like this:

    $html_code = '<pre><span style="color:red">prompt $</span><span style="color: green">command</span></pre>';
    $renderer->generateODTfromHTMLCode($html_code, null);

I did not test it. If it gives you a crash rather try:

    $html_code = '<pre><span style="color:red">prompt $</span><span style="color: green">command</span></pre>';
    $options = array();
    $renderer->generateODTfromHTMLCode($html_code, $options);

Please report the result. I could tweak the function ODTUtility::generateODTfromHTMLCode a bit if the result is not what you expect.
Avatar
schplurtz (Moderator) #6
Member since Nov 2009 · 493 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
Thank you LarsDW223.
Such quick answer is really appreciated. And also it's fun to have a branch by my name.

I'll certainly report results.
Avatar
schplurtz (Moderator) #7
Member since Nov 2009 · 493 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
generateODTfromHTMLCode cannot parse <pre> tags.
$options = array();
$renderer->generateODTfromHTMLCode($html_code, $options);
[Image: https://framapic.org/dfn5c2EGisme/wutYeTmetnjM.png]


But if I copy some options from renderer_plugin_odt_page::_highlight() and remove the <pre> and </pre> tags, then generateODTfromHTMLCode produces some nice results :

A monospace font is used, spaces are preserved and, the CSS has been automagically loaded and font colors are applied. This is great.
$options=array();
$options ['escape_content'] = 'false';
$options ['space'] = 'preserve';
$options ['media_selector'] = 'screen';
$options ['element'] = 'pre';
$renderer->generateODTfromHTMLCode($this->html, $options);

I am pleased with these results, so no need to do anything more.

One very minor point, maybe for later (or never). <code> blocks have an ODT style of "preformatted text" and thus a light blue background. But the block generated by generateODTfromHTMLCode() has no particular ODT style.

Anyway, thanks again.

[Image: https://framapic.org/eTKRQcoZ4ypq/wA4tAmLxKjVd.png]
Avatar
LarsDW223 #8
Member since Sep 2014 · 447 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
generateODTfromHTMLCode cannot parse <pre> tags.

I will try to improve that. I guess I can do some refactoring and share code between function preformatted() and generateODTfromHTMLCode().

But the block generated by generateODTfromHTMLCode() has no particular ODT style.

I think some style names could be passed in the options array. I need to review that and add some documentation.

Would you be willing to re-test it once I have got a new version?
Avatar
schplurtz (Moderator) #9
Member since Nov 2009 · 493 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
I think some style names could be passed in the options array. I need to review that and add some documentation.
Ah ah. Ok, I took a more indepth look at the ODT code and I could get a basic understanding of how style works. But it seems to be more complex, documentation would be welcome.
  1. if(!$renderer->styleExists('Command Line Interface')) { //FIXME: are styles looked up by style-display-name?
  2.  $style=array('style-display-name' => 'Command Line Interface',
  3.               'background-color' => '#feacdc' // some kind of pink
  4.                      );
  5.  $renderer->createParagraphStyle( $style );
  6. }
  7. $options['p_style']='Command Line Interface';
  8. ....
  9. generateCode....

Would you be willing to re-test it once I have got a new version?
Of course.
The author has attached one file to this post:
cli-style.png 52.1 kBytes
You have no permission to open this file.
This post was edited on 2019-07-08, 20:57 by schplurtz.
Avatar
LarsDW223 #10
Member since Sep 2014 · 447 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
But it seems to be more complex, documentation would be welcome.

I did write some phpDoc documentation blocks long time ago. If you install and start phpDoc in the odt plugin folder it will write the documentation of the functions comment blocks into sub-dir /documentation/phpDoc/index-html. Unfortunately phpDoc seems to be broken with my PHP version. I only get the main page and it seems to abort at some point.
Avatar
LarsDW223 #11
Member since Sep 2014 · 447 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
There is also some more documentation in the github wiki but I fear it might be outdated, see https://github.com/LarsGit223/dokuwiki-plugin-odt/wiki/dok….
Avatar
LarsDW223 #12
Member since Sep 2014 · 447 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
In your branch I added a function comment block to ODTUtility::generateODTfromHTMLCode(). Please have a look.
Avatar
LarsDW223 #13
Member since Sep 2014 · 447 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
//FIXME: are styles looked up by style-display-name?

No, because the style-display-name is only present/set for common styles which you then e.g. also see in the LibreOffice GUI. But styles generated from direct formatting also need to be referenceable. So every style has got a style-name. And common styles additionally have a style-display-name which can be presented to the user.

E.g. see below the ODT element defining the paragraph style for preformatted text:

<style:style style:name="Preformatted_20_Text" style:display-name="Preformatted Text" style:family="paragraph" style:parent-style-name="Standard" style:class="html">
    <style:paragraph-properties fo:margin-top="0cm" fo:margin-bottom="0.199cm" loext:contextual-spacing="false" style:join-border="false"/>
    <style:text-properties style:font-name="Bitstream Vera Sans Mono" fo:font-family="&apos;Bitstream Vera Sans Mono&apos;" style:font-family-generic="modern" style:font-pitch="fixed" style:font-name-asian="Bitstream Vera Sans Mono" style:font-family-asian="&apos;Bitstream Vera Sans Mono&apos;" style:font-family-generic-asian="modern" style:font-pitch-asian="fixed" style:font-name-complex="Bitstream Vera Sans Mono" style:font-family-complex="&apos;Bitstream Vera Sans Mono&apos;" style:font-family-generic-complex="modern" style:font-pitch-complex="fixed"/>
</style:style>

And now see below the style for underlined text spans (direct formatting, not editable in the LibreOffice GUI - so NO style:display-name attribute):

<style:style style:name="underline" style:family="text">
  <style:text-properties style:text-underline-style="solid"
     style:text-underline-width="auto" style:text-underline-color="font-color"/>
</style:style>

In ODTDefaultStyles::getStyleName() you can see the mapping from known internal aliases to real ODT-XML style names defined in styles.xml.

In your example you wrote:

    if(!$renderer->styleExists('Command Line Interface')) { //FIXME: are styles looked up by style-display-name?
     $style=array('style-display-name' => 'Command Line Interface',
                  'background-color' => '#feacdc' // some kind of pink
                         );
     $renderer->createParagraphStyle( $style );
    }
    $options['p_style']='Command Line Interface';
    ....
    generateCode....

This will not work as you only specified the style-display-name but not the style-name used for referencing a style. As it is not given, the ODT plugin will generate one dynamically. So you should change your code to:

    if(!$renderer->styleExists('Command Line Interface')) { //FIXME: are styles looked up by style-display-name?
     $style=array('style-name' => 'Command Line Interface',
                  'style-display-name' => 'Command Line Interface',
                  'background-color' => '#feacdc' // some kind of pink
                         );
     $renderer->createParagraphStyle( $style );
    }
    $options['p_style']='Command Line Interface';
    ....
    generateCode....
Avatar
LarsDW223 #14
Member since Sep 2014 · 447 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
One additional remark:
Of course style names MUST be unique. So it would make sense to add a plugin name as a prefix or something. But you could have duplicate style-display-names. That would be confusing for the user but would not break anything.
Avatar
schplurtz (Moderator) #15
Member since Nov 2009 · 493 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
That's a lot to process. I'm going to pull your commit and try the phpdoc. I did not notice the github wiki, going to read that too. I almost never check wiki on github and my first reflex was of course dokuwiki.org ;-)

Thanks for the details about the styles.
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Page:  1  2  next 
Go to forum
Imprint
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Current time: 2019-11-14, 21:14:33 (UTC +01:00)