JeroenDeDauw / GitHub

MediaWiki extension that allows embedding the content of files hosted in GitHub git repositories
https://professional.wiki/
Other
32 stars 20 forks source link

Source code highlighting #8

Closed warrenseine closed 7 years ago

warrenseine commented 10 years ago

I'm using this extension to display source code on a documentation wiki so it stays up to date.

I've had to hack the extension to re-render the rendered content with a <source> tag around it, which will be rendered by another extension I use, Syntax Highlight GeSHi if I remember correctly. This works fine if my case.

I suppose adding a dependency to the project would not be acceptable, so I'm not going to make a pull request. Instead, I'm simply going to post the patch here so it might be useful to somebody someday.

diff --git src/GitHub/GitHubParserHook.php src/GitHub/GitHubParserHook.php
index fa21545..7135c90 100644
--- src/GitHub/GitHubParserHook.php
+++ src/GitHub/GitHubParserHook.php
@@ -33,7 +33,7 @@ class GitHubParserHook implements HookHandler {
    public function handle( Parser $parser, ProcessingResult $result ) {
        $this->setFields( $result );

-       return $this->getRenderedContent();
+       return $this->getRenderedContent($parser);
    }

    protected function setFields( ProcessingResult $result ) {
@@ -44,12 +44,16 @@ class GitHubParserHook implements HookHandler {
        $this->branchName = $params['branch']->getValue();
    }

-   protected function getRenderedContent() {
+   protected function getRenderedContent( Parser $parser ) {
        $content = $this->getFileContent();

        if ( $this->isMarkdownFile() ) {
            $content = $this->renderAsMarkdown( $content );
-       }
+       } else {
+           $content = '<source>' . $content . '</source>';
+           $content = $parser->recursiveTagParse( $content, null );
+       }

        return $content;
    }
JeroenDeDauw commented 10 years ago

A pull request is welcome. The adding of the source tag would need to be optional though.

Also, just sticking a tag source tag around all the content is a bit hacky.

In case it needs to go around the whole content, is is not possible to just insert it around the call to the github parser function?

Perhaps a parameter can be added to allow surrounding the thing with a tag? ie

{{#github: ... |tag=source}}

Or perhaps like this for greater flexibility?

{{#github: ... |intro=<source>|outro=</source>}}

Or perhaps given source code is such a typical thing to have on GH, having a flag makes more sense.

{{#github: ... |highlightsyntax=yes}}

With such a flag, the extension could surround with the "pre" tag by default, and use "source" if it detects GeSHi is installed. And this way it could also support other highlighting extensions easily.

I'll be happy to review a PR. If it gets merged you won't have to re-apply your changes every time you update, and other people will have an easier time using the functionality.

warrenseine commented 10 years ago

I like the last idea. Putting that in my todo list :)

JeroenDeDauw commented 9 years ago

I guess you have a very big todo list :)

kettner commented 9 years ago

GeSHi functionality would really help! Also, right now the imported text from github is rendered as 'wiki text', so e.g. "#" becomes a '1'. Is there anyway to change that as well?

JeroenDeDauw commented 9 years ago

I've created a new ticket to track that enhancement request: https://github.com/JeroenDeDauw/GitHub/issues/20 (it is not currently possible to do this)

warrenseine commented 9 years ago

@JeroenDeDauw I do :)

I didn't take the time to look at it and it was always low priority anyway.

I also switched my documentation system to GitHub, dropping MediaWiki in the mean time. So yeah, I'm not going to contribute this addition, sorry :)

You can close or keep it open if you think it still makes sense.

JeroenDeDauw commented 9 years ago

@warrenseine thanks for th update. The enhancement itself makes sense, so keeping this open.

danyill commented 8 years ago

I was hoping to simply preparse the text from this extension using e.g.:

{{#tag:syntaxhighlight|{{#github:Time-Current Grading.sagews|danyill/time-current-grading}}|lang=python}}

Unfortunately that didn't work for me. See also here

Would this be a better general purpose solution allowing a range of post-processing options?

JeroenDeDauw commented 8 years ago

That'd be nice as well, and generally I think it's good to keep programs simple and making them chainable. Not sure this would always work here though. The markdown output can contain HTML such as tables. Does the syntaxhighlight tag deal with those correctly? I kinda doubt it.

JeroenDeDauw commented 7 years ago

This was implemented in 1.2