Geshi with Markdown - php

Im trying to get GeSHi to work with markdown.
A simple use for Geshi is as follows:
$geshi = new GeSHi($message, 'c');
print $geshi->parse_code();
The above code takes in the whole of message and turns it into Highlighted code
I also have my Markdown Function
print Markdown($message);
I was trying to use call back function to preg_match the <pre> tags returned from markdown and run the geshi->parse_code(); function on the returned values
Here is my code
print preg_replace_callback(
'/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/gism',
create_function(
// single quotes are essential here,
// or alternative escape all $ as \$
'$matches',
'$geshi = new GeSHi($matches[0], \'php\'); return $geshi->parse_code()'
),
Markdown($blog_res['message']));
Am i on the right track?
Is My Regex right? it works on http://gskinner.com/RegExr/
Thanks for the help

for future reference, you might want to check out my plugin for this:
https://github.com/drm/Markdown_Geshi
It is based on the regular markdown plugin adding a block marked with a shebang to highlight code, like this:
#!php
<?php print('This is PHP code'); ?>
Works pretty well and I use it on my own blog regularly.

it was the regex :(
instead of
/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/gism
use (remove the global flag)
/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/ism
But if you are using markdown, you have to remember to compensate for the code blocks that are on thier own therefore you need to replace only the ones that are in the format of <pre><code>...MyCode</code></pre> and leave out Hello <code>MyCode</code> Therefore you need the following
'/<pre.*?><code.*?>(.*?[<pre.*?><code.*?>.*<\/code><\/pre>]*)<\/code><\/pre>/ism',

I understand that you [were] looking to extend Markdown, adding support for GeSHi syntax highlighting. Beautify does this and more. For example, it can render graphs in DOT.
Beautify's approach to GeSHi code blocks differs from drm/Markdown_Geshi in that "fences" are used. For example:
~~~ php
<?php print('This is PHP code'); ?>
~~~
I'm not sure if Beautify was around back when this question was active, but it seemed worthy of mention in an answer.

Related

Replace // comments by /* comments */ Except in URLs [duplicate]

I need to remove the comment lines from my code.
preg_replace('!//(.*)!', '', $test);
It works fine. But it removes the website url also and left the url like http:
So to avoid this I put the same like preg_replace('![^:]//(.*)!', '', $test);
It's work fine. But the problem is if my code has the line like below
$code = 'something';// comment here
It will replace the comment line with the semicolon. that is after replace my above code would be
$code = 'something'
So it generates error.
I just need to delete the single line comments and the url should remain same.
Please help. Thanks in advance
try this
preg_replace('#(?<!http:)//.*#','',$test);
also read more about PCRE assertions http://cz.php.net/manual/en/regexp.reference.assertions.php
If you want to parse a PHP file, and manipulate the PHP code it contains, the best solution (even if a bit difficult) is to use the Tokenizer : it exists to allow manipulation of PHP code.
Working with regular expressions for such a thing is a bad idea...
For instance, you thought about http:// ; but what about strings that contain // ?
Like this one, for example :
$str = "this is // a test";
This can get complicated fast. There are more uses for // in strings. If you are parsing PHP code, I highly suggest you take a look at the PHP tokenizer. It's specifically designed to parse PHP code.
Question: Why are you trying to strip comments in the first place?
Edit: I see now you are trying to parse JavaScript, not PHP. So, why not use a javascript minifier instead? It will strip comments, whitespace and do a lot more to make your file as small as possible.

Make certain part of string bold using preg_replace()

I have a string of a comment that looks like this:
**#First Last** rest of comment info.
What I need to do is replace the ** with <b> and </b> so that the name is bold.
I currently have this:
$result = preg_replace('/\*\*/i', '<b>$0</b>', $comment);
But that results in:
"<b>**</b>#First Last<b>**</b> rest of comment info"
Which is obviously not what I need. Does anyone have any suggestions on how to achieve this?
A better idea would be to use a markdown parser library which does this for you.
Improper use of the regular expressions can lead to security vulnerabilities with unclosed tags etc..
However, a simple approach could be this:
$result = preg_replace('/\*\*(.+)\*\*/sU', '<b>$1</b>', $comment);
The U means ungreedy and takes the first possible match.
The s modifies . to be any character, including newline.
(see modifiers page)
This also works with an example text like this:
**#First Last** rest of comment info **also bold**.
The next line also **has a bold example**. This spans **two
lines**.
I am sure there are counter examples which are broken with this. Again, please use a proper markdown library to do this for you (or copy their implementation if the LICENSE allows it).

How to remove tags and html entities in php

I want to remove all the tags (but keeps img, sub, sup) and styles from my code and also remove all the html entities (but keeps & (&) and © (©)) using REGEX. but I don't know how it's use can any one guide me.
Thank you in advance.
For PHP take a look at the php docs at http://php.net/manual/de/function.preg-replace.php
For Regexp in general check http://www.regular-expressions.info/
For live testing take a look at https://regex101.com/, it is a really good testing tool and explains the whole expression piece by piece.
As you want to keep your styles, strip_tags won't take you the whole way.
Try filter_var
You can see more filters on:
http://php.net/manual/en/filter.filters.sanitize.php
echo filter_var('coco <p>©&</p>',FILTER_SANITIZE_STRING);

vim php_javascriptInStrings option?

I noticed that the syntax/php.vim file on my ubuntu machine has a php_htmlInStrings option. I can turn this option on to display HTML syntax highlighting within strings in my php files, which is great. I would also like to do javascript syntax highlighting within strings in a php file. Does anybody know if this can be done and if so how can I do it?
edited - added extra possibilities
I should also mention that I would be happy with a solution where i have to parse all my javascript strings though a php function before outputting the result. This might get around the problem suggested by conner below where vim has trouble deciding if the string contains javascript. for example:
$js = "some regular text which is not javascript##now vim has
detected that this part is javscript##back to regular text";
parse($js);
function parse($str)
{
return str_replace('##', '', $str);
}
The reason I would be happy to do this is because I will probably be incorporating a html/css/js variable minifier into my project which will be doing substitutions on strings anyway.
Of course if there is a vim-specific equivalent character for ## which will not show up in the source code and would not need to be filtered out then this would be preferable...
re-edit 2
As per conner's solution below, the desired effect can be achieved like so:
$js = "<script>some javascript</script>";
(with :let php_htmlInStrings=1 in vim). If somebody can show me the vim script required to get the javascript syntax highlighting to show up in the following string then I will award them the answer:
$js = /*<script>*/"some javascript"/*</script>*/;
I think the general problem with this is that vim needs a way to differentiate between the javascript and HTML highlighting. In HTML files, vim determines this based on the <script></script> tags within it to apply the javascript highlighting. If you put <script></script> tags in your string you'll see that this is the case. However, if you take those away then vim has no way of knowing if the content in your string is HTML or javascript. You could remedy this by editing adding something to signify that it's javascript that hopefully wouldn't effect the resultant code, but this is tricky. You can see where the HTML file is setting the <script></script> tag specification on line 167 of $VIMRUNTIME/syntax/html.vim. It looks like this:
syn region javaScript start=+<script[^>]*>+ keepend end=+</script>+me=s-1 contains=#htmlJavaScript,htmlCssStyleComment,htmlScriptTag,#htmlPreproc
Have you tried this php.vim syntax file?

how to remove the comment line starts with // and not the url like http:// using preg_replace

I need to remove the comment lines from my code.
preg_replace('!//(.*)!', '', $test);
It works fine. But it removes the website url also and left the url like http:
So to avoid this I put the same like preg_replace('![^:]//(.*)!', '', $test);
It's work fine. But the problem is if my code has the line like below
$code = 'something';// comment here
It will replace the comment line with the semicolon. that is after replace my above code would be
$code = 'something'
So it generates error.
I just need to delete the single line comments and the url should remain same.
Please help. Thanks in advance
try this
preg_replace('#(?<!http:)//.*#','',$test);
also read more about PCRE assertions http://cz.php.net/manual/en/regexp.reference.assertions.php
If you want to parse a PHP file, and manipulate the PHP code it contains, the best solution (even if a bit difficult) is to use the Tokenizer : it exists to allow manipulation of PHP code.
Working with regular expressions for such a thing is a bad idea...
For instance, you thought about http:// ; but what about strings that contain // ?
Like this one, for example :
$str = "this is // a test";
This can get complicated fast. There are more uses for // in strings. If you are parsing PHP code, I highly suggest you take a look at the PHP tokenizer. It's specifically designed to parse PHP code.
Question: Why are you trying to strip comments in the first place?
Edit: I see now you are trying to parse JavaScript, not PHP. So, why not use a javascript minifier instead? It will strip comments, whitespace and do a lot more to make your file as small as possible.

Categories