Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-11-02 09:18:59

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Code syntax highlighting of bc in txp:body with textile?

On the forum we have a little helper so that we can do:

bc..{space}{newline}
txp{newline}
txp:code here …

(or html, php, css, js in place of txp)

That works great for highlighting code, even though we have no actual txp-specific syntax highlighter at this time.

For that there’s a little helper file that converts the textile into the prism class for the pre tag (later the js turns that from language-txp -> language-html).

Can we do something similar for regular Textpattern sites? Prism and textile are both part of a standard Textpattern installation. Is there a way to add the same hook as used here in the forum to Textpattern’s textiling?


TXP Builders – finely-crafted code, design and txp

Offline

#2 2019-11-02 11:38:45

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,134
GitHub

Re: Code syntax highlighting of bc in txp:body with textile?

jakob wrote #319900:

On the forum we have a little helper so that we can do:

bc..{space}{newline}...

(or html, php, css, js in place of txp)

That works great for highlighting code, even though we have no actual txp-specific syntax highlighter at this time.

[…]

Can we do something similar for regular Textpattern sites?

+1 from me, great idea.

Offline

#3 2019-11-02 12:56:34

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: Code syntax highlighting of bc in txp:body with textile?

gaekwad wrote #319901:

+1 from me, great idea.

+1, I often catch myself typing bc. txp too.

Offline

#4 2019-11-02 14:16:38

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Code syntax highlighting of bc in txp:body with textile?

Glad you both agree :-) What can we do about? :~))

Two Three options spring to mind:

  1. Incorporate that directly into textile (is the language-… class general enough not to be tied to prism.js alone?)
  2. Add that facility as a textile plugin (is that a thing? I vaguely remember Netcarver mentioning that possibility)
  3. Make a txp plugin / shortcode-form out of Jukka’s preg_replace helper file.

TXP Builders – finely-crafted code, design and txp

Offline

#5 2019-11-02 17:22:54

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Code syntax highlighting of bc in txp:body with textile?

Well, I’ve had a go at making a simple plugin that simply adds a syntaxhint attribute to txp:body but it’s not working as it should it is working thanks to etc’s input. Thank you!

// TXP 4.7 tag attribute registration
if (class_exists('\Textpattern\Tag\Registry')) {
    // Attribute mode
    if(method_exists('\Textpattern\Tag\Registry', 'registerAttr'))
        Txp::get('\Textpattern\Tag\Registry')->registerAttr('jcr_syntaxhint', 'syntaxhint');
}

function jcr_syntaxhint($atts, $thing = null)
{
    // borrowed wholesale from @gocom's code for the textpattern forum
    static $extra_code_language_identifiers = array(
        'apacheconf',
        'clike',
        'coffeescript',
        'css',
        'git',
        'haml',
        'html',
        'javascript',
        'js',
        'json',
        'less',
        'markdown',
        'markup',
        'nginx',
        'perl',
        'php',
        'ruby',
        'sass',
        'scss',
        'sql',
        'stylus',
        'textile',
        'txp',
        'yaml',
    );

    extract(lAtts(array(
        'syntaxhint' => ''
    ), $atts, false));

    if ($syntaxhint) {
        $code_language_identifiers = implode('|', $extra_code_language_identifiers);

        return preg_replace(
            '@<pre><code>(?:\/\/|#|;)?(?:\s+)?('.$code_language_identifiers.')[\n\r]+@',
            '<pre class="prism language-$1" data-language="$1"><code>',
            $thing
        );

    } else {
        return $thing;
    }
}

TXP Builders – finely-crafted code, design and txp

Offline

#6 2019-11-02 22:09:03

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: Code syntax highlighting of bc in txp:body with textile?

I have edited the code, please try. But you will see the result only in page source, because .prism class does nothing on the public side.

Offline

#7 2019-11-02 22:11:39

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Code syntax highlighting of bc in txp:body with textile?

Got it working by taking out the code identifier variable and making it a global (probably not great but…):

... code removed. please see post above ...

But it works now as here on the forum. This just adds the class. It doesn’t bind in prism.js.


TXP Builders – finely-crafted code, design and txp

Offline

#8 2019-11-02 22:13:20

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Code syntax highlighting of bc in txp:body with textile?

Oh thank you, I’ll try your variant too! Hold on…


TXP Builders – finely-crafted code, design and txp

Offline

#9 2019-11-02 22:19:01

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: Code syntax highlighting of bc in txp:body with textile?

jakob wrote #319911:

Oh thank you, I’ll try your variant too! Hold on…

It’s the same, less a global.

Offline

#10 2019-11-02 22:24:21

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Code syntax highlighting of bc in txp:body with textile?

etc wrote #319912:

It’s the same, less a global.

Yours is better not only in that respect. Also in outputting the body when syntaxhint is off. But I needed to remove $codeLanguageIdentifiers as a parameter from the function (txp seems to interpret that as $thing as I discovered which was throwing off the preg_replace code).

Thank you again for helping!


TXP Builders – finely-crafted code, design and txp

Offline

#11 2019-11-03 09:13:18

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: Code syntax highlighting of bc in txp:body with textile?

jakob wrote #319913:

(txp seems to interpret that as $thing as I discovered which was throwing off the preg_replace code).

Every tag is passed attributes as its first argument and $thing (even if named differently) as the second one. But I think your plugin should act on article save rather than as global attribute.

Offline

#12 2019-11-03 09:45:06

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Code syntax highlighting of bc in txp:body with textile?

etc wrote #319914:

Every tag is passed attributes as its first argument and $thing (even if named differently) as the second one.

As I gathered :-)

But I think your plugin should act on article save rather than as global attribute.

That’s a much better idea, I agree: process it only once instead of on every page render. I’ll have to investigate that but probably won’t get to that today.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB