Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
1.2k views
in Plugins by
edited by

I need help with creating Syntax Highlight Plugin. I created the plugin containing one module of viewer type. But I noticed, that qa-viewer-basic.php takes precedence over my plugin. I don't want to change the content format, because it is still html. I would rather make a class that extends the qa-viewer-basic. Could you please give me some clarification on that?

EDIT:

I've finally decided to use new custom format "codehtml" to force my plugin to work. But now  I have another problem. In wysiwyg editor plugin you use $qa_content['script_src'] to include some js code within the header. I'm trying to do the same in my viewer module, but it simply doesn't work. Here's the code:

        function get_html($content, $format, $options)
        {
            if ($format=='codehtml' || $format=='html') {
                $qa_content['script_src'][]='./qa-plugin/syntax-highlighter/js/scripts/shCore.js';
                $html=$content;

What's wrong? Is there another way to include some javascript in header using plugins?

1 Answer

0 votes
by

Hi there - great that you're working on this.

First, if you want to go back to the original method, i.e. using the 'html' format in the database, you can make your viewer take precedence over qa_viewer_basic by returning a value greater than 1.0 from the calc_quality(...) function in your viewer's class. Of course you should only do this if the $format parameter you're supplied is 'html'.

Second, the get_html(...) function in a viewer class can't access the global $qa_content variable, since it's not passed a reference to it. Instead, you can embed the Javascript load inside a <SCRIPT...> tag within the HTML that is returned by your function. Also, when buliding the URL for this Javascript file, you should start from the relative URL that was passed to your module's load_module(...) function to ensure it works on all pages. And finally, make sure you only enter your Javascript tag once per Q2A page, since your viewer could be called multiple times for multiple answers. A PHP global variable can solve this for you.

Hope this helps - feel free to ask any more questions about this.

by
Thanks for quick response. I'm getting to work, and I'll let you know about the results soon.
by
edited by
I've finished the the alfa version. You can test it here:
http://quanda.pl/index.php?qa=19&qa_1=test-kodu-%C5%BAr%C3%B3d%C5%82owego
I'm thinking about the way to handle html tags which for now are stripped out. But in general - it works! :) Thanks Gid! I will work to make it better and after final release I would share the plugin with the community.
by
There were small problems with recursion in window.onload function, but now it seems to be ok :). Now I want to find a way to change the "pre" keyword to sourcecode in square brackets like used in wordpress.com. Any ideas?
by
Well, I've basically done. Now I can use the following syntax:
[source language='css']code here[/source]
[code language='css']code here[/code]
[sourcecode lang='css']code here[/sourcecode]
[source lang='css']code here[/source]
[code lang='css']code here[/code]
[sourcecode='css']code here[/sourcecode]
[source='css']code here[/source]
[code='css']code here[/code]
[lang='css']code here[/lang]
[css]code here[/css] (or any of the supported language)
The code still need some testing and optimization, I would be gratefull for tests and comments. Gid, do you think, you could put the plugin somewhere on you website to download and test for others?
by
Hi Ernest - sorry for the delay - I just saw your comment. Would you like to put your plugin code online somewhere so I can link to it from the Q2A site?
by
Hi Gidgreen, of course, it is available for download at http://quanda.pl/index.php?qa=download
and this is a direct link to zip file: http://quanda.pl/download/syntax-highlighter.zip
by
Thanks - I've added the link to the page since the explanation is helpful: http://www.question2answer.org/install.php#addons
by
Roger that :) Thanks
...