Im currently working on a system that has a comment system integrated, the system is running on Codeigniter so im looking to create a markdown library but with really minimal features.
The features im looking to have is
Autolinking
Bold *bold*
Italic _italic_
And that's practically it, The post data will be run through Codeigniter's XSS Class before it goes to the mark down class
So my question is what's the best way to do this, should i be using a library out there and disabling certain features, should I build this from scratch, if so, how should i build the class and what things should I take into account.
I was in a similar situation recently, where I wanted to support some kind of markup (BB, Markdown, etc). It turns out nothing has been done with BBCode for about 100 years, and it's dead easy to write a regex parser for it (for well-formed markup at least) so i wrote a really bare bones function to do just that.
My version also includes images, codes, and color support as well as nested tags ([b][i]bold and italic[/i][/b]).
function parseBBCode($string){
$search = array(
'/\[b\](.*?)\[\/b\]/',
'/\[i\](.*?)\[\/i\]/',
'/\[u\](.*?)\[\/u\]/',
'/\[img\](.*?)\[\/img\]/',
'/\[url\=(.*?)\](.*?)\[\/url\]/',
'/\[code\](.*?)\[\/code\]/',
'/\[color\=(.*?)\](.*?)\[\/color\]/'
);
$replace = array(
'<strong>\\1</strong>',
'<em>\\1</em>',
'<u>\\1</u>',
'<img src="\\1">',
'\\2',
'<code>\\1</code>',
'<span style="color:\\1;">\\2</span>'
);
$new = preg_replace($search, $replace, $string);
return nl2br($new);
}
You can begin with PHP Markdown class ?
or the one for CI.
And If I may suggest, you can also try MarkItUp as front end..
For me the easiest way to integrate Markdown is by simply
putting markdown.php from Michel Fortrin into my Application/helpers/ folder,
rename it to markdown_helper.php
load it with $this->load->helper('markdown');
...just in case someone - like me - stumbles upon this old thread again :)
Related
Im migrating big Wordpress page to custom CMS. I need to extract information from big (20MB+) XML file, exported from Wordpress.
I don't have any experience in XML under PHP and i don't know how to start reading file.
Wordpress file contains structures like this:
<excerpt:encoded><![CDATA[Encoded text here]]></excerpt:encoded>
and i don't know how to handle this in PHP.
You are probably going to do fine with simplexml:
$xml = simplexml_load_file('big_xml_file.xml');
foreach ($xml->element as $el) {
echo $el->name;
}
See php.net for more info
Unfortunately, your XML example didn't come through.
PHP5 ships with two extensions for working with XML - DOM and "SimpleXML".
Generally speaking, I recommend looking into SimpleXML first since it's the more accessible library of the two.
For starters, use "simplexml_load_file()" to read an XML file into an object for further processing.
You should also check out the "SimpleXML basic examples page on php.net".
I don't have any experience in XML under PHP
Take a look at simplexml_load_file() or DomDocument.
<excerpt:encoded><![CDATA[Encoded text here]]></excerpt:encoded>
This should not be a problem for the XML parser. However, you will have a problem with the content exported by WordPress. For example, it can contain WordPress shortcodes, which will come across in their raw format instead of expanded.
Better Approach
Determine if what you are migrating to supports an export from WordPress feature. Many other systems do - Drupal, Joomla, Octopress, etc.
Although Adam is Absolutely right, his answer needed a bit more details. Here's a simple script that should get you going.
$xmlfile = simplexml_load_file('yourxmlfile.xml');
foreach ($xmlfile->channel->item as $item) {
var_dump($item->xpath('title'));
var_dump($item->xpath('wp:post_type'));
}
simplexml_load_file() is the way to go creating an object, but you will also need to use xpath as WordPress uses name spaces. If I remember correctly SimpleXML does not handle name space well or at all.
$xml = simplexml_load_file( $file );
$xml->xpath('/rss/channel/wp:category');
I would recommend looking at what WordPress uses for importing the files.
https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-importer.php
Wondering how to reach a css file like this one from css-tricks.com
http://cdn.css-tricks.com/wp-content/themes/CSS-Tricks-9/style.css?v=9.5
Not sure if he is using php to accomplish this or not. I've been reading countless articles with no luck.
Also, is it something automated that spits out the version number after the .css? Been seeing it around and wondered how to achieve a clean css file.
Any help is appreciated! Thanks.
It's simple enough to use an editor with Search/Replace and strip out all the unnecessary spaces. For instance, when I write CSS I only use spaces to separate keywords - I use newlines and tabs to format it legibly. So I could just replace all tabs and newlines with the empty string and the result is "minified" CSS like the one above.
The version number is a fairly common cache trick. It doesn't affect anything server-side, but the browser sees it as a new file, and caches it as such. This makes it easy to purge the cache of all users when an update is made. Personally, though, I use a PHP function to append "?t=".filemtime($file) (in other words, the timestamp that the file was modified) automatically, which saves me the trouble of manually updating version numbers.
Here is the exact code I use to automatically append modification time to JS and CSS files:
<?php
ob_start(function($data) {
chdir($_SERVER['DOCUMENT_ROOT']);
return preg_replace_callback(
"(/(?:js|css)/.*?\.(?:js|css))",
// all the relevant files are in /js and /css folders of the root
function($m) {
if( file_exists(substr($m[0],1)))
return $m[0]."?t=".filemtime(substr($m[0],1));
else return $m[0];
},
$data
);
});
?>
I would avoid to do it manually because you may corrupt your css.
There are good tools available which will solve such problems for you without to be tricky.
An excellent solution is Assetic which is an assets manager and allow you to filter (minify, compress) using various tools (yuicompressor, google closure, etc..).
It is currently bundle by default with Symfony2 but may be used standalone in any PHP Project.
I've successfully implemented it in a Zend Framework project.
I've been looking around for some way to either code up links using bbcode or manually convert a url in a specified message to a link. BBCodes to me are just getting a little old. Although, are still massively heavily used for such things as smileys etc.
I'd be looking to probably do a mixture of the two functionalities.
Can anyone advise on something they use or have used recently for prettifying a messaging system, so to speak.
As far as converting links, Codeigniter's got you covered with the url helper:
auto_link()
Automatically turns URLs and email addresses contained in a string
into links. Example: $string = auto_link($string);
The second parameter determines whether URLs and emails are converted
or just one or the other. Default behavior is both if the parameter is
not specified. Email links are encoded as safe_mailto() as shown
above.
As for smilies, that's covered as well. There is actually a smiley helper:
If you give up and want to parse bbcode, here's a helper written by Phil Sturgeon (a lead Codeigniter developer): https://github.com/bcit-ci/CodeIgniter/wiki/BBCode-Helper
If you wanted to go with something client side for BBCode interpritation, I've written an extendible BBCode parser in JavaScript.
It has all of the standard BBCode tags, but if your messaging system needed some new tags for certain kinds of URL manipulation they could be easily added. As an example, for a smilies tag you could extend it like this:
"smiley": {
openTag: function(params,content) {
if (content === ":)") {
return "<img src='smiley.png'/>";
} else if (content === ":(") {
return "<img src='frown.png'/>";
} else {
return "";
}
},
closeTag: function(params,content) {
return "";
}
}
And then the BBCode would look something like:
[smiley]:)[/smiley]
And the HTML code it would generate from that would look like this:
<img src='smiley.png'/>
This might be more work than what you want and you may not want your own custom tags for your messaging system, but I figured I'd mention it just in case.
I love markdown, and I have the Wordpress markdown-for-wordpress-and-bbpress parsing markdown in my posts and comments.
However, I've noticed that Wordpress saves the comments rendered in html format. This makes it more difficult to go back and edit comments. How can I get wordpress to save comments in markdown format?
I couldn't find a plugin for it. Maybe there's an easy php hack?
Edit:
Maybe it's not builtin to wordpress. Comments normally not saved with any markup without the markdown plugin. Could be a markdown-for-wordpress-and-bbpress "feature" / accident?
Cross-posted to wordpress.stackexchange.com. BAinternet had some good ideas of saving markup for comments like in the markup-on-save plugin, but no working solution yet.
Partial hack
may help? May be theme-dependent. Lists still get saved rendered sometimes.
In wp-content/plugins/markdown-for-wordpress-and-bbpress/markdown.php comment out the pre_comment_content markdown filter
if (MARKDOWN_WP_COMMENTS) {
remove_filter('comment_text', 'wpautop', 30);
remove_filter('comment_text', 'make_clickable');
#HACK don't save comments rendered in HTML
#add_filter('pre_comment_content', 'Markdown', 6);
add_filter('pre_comment_content', 'mdwp_hide_tags', 8);
add_filter('pre_comment_content', 'mdwp_show_tags', 12);
add_filter('get_comment_text', 'Markdown', 6);
add_filter('get_comment_excerpt', 'Markdown', 6);
add_filter('get_comment_excerpt', 'mdwp_strip_p', 7);
Nice question. As this feature is not available in the Wordpress plugin, you'll need to do some hackery at least to stop it from saving in HTML format, which you have done.
Now you need for when the comments are displayed to process that markdown into HTML. So let's use the comment_text hook:
<?php add_filter('comment_text', 'Markdown'); ?>
If you don't want your original code to feel like "hackery" -- turn it into a feature. Add a config option to the Markdown.php $save_format = 'html' or $save_format = 'markdown' then check if you want to execute the stripper function or not. In fact, you could be really smart and turn all of this into a function inside of Markdown.php (and remember to tell the author about your new feature, he might even update his original code ;)
function set_save_format($format) {
if ($format == 'markdown') {
// Ok we need to change the format of any comments output to html:
add_filter('comment_text', 'Markdown');
}
}
I guess u can use the http://adambrown.info/p/wp_hooks/hook/comment_save_pre-hook to manipulate the data.
I have been looking online for a tutorial to build a template engine. I know there are many engines that exist, like smarty, twig, and pattemplate, that could do exactly what I want, but I am looking to learn how to build one. I started with a template engine that added strings to an array and then displayed the array. Since then I built one using eval() (see below).
<// Define links & folders
define("ROOT_HTTP", "http://" . $_SERVER['HTTP_HOST'] . "/preprocessor");
define("TEMPLATE", "/template");
// Get the template file
$template = file_get_contents("template/template.php");
// Replace
$template = str_replace("<x Title x>", displayTitle(), $template);
$template = str_replace("<x Menu x>", displayMenu(), $template);
$template = str_replace("<x Content x>", displayContent(), $template);
$result = #eval("?>" . $template . "<?");
function displayMenu(){
return "Link1<br />" .
"Link2<br />" .
"Link3<br />";
}
function displayTitle(){
return "Site Title <?php echo date(\"m-d-y\", time()); ?>";
}
function displayContent(){
return file_get_contents("content.php");
}
It works fairly well but its not what I am looking to achieve. I would like to build something that is like the Joomla template with tags like <jdoc:include type="component" />. I would also like it to be able to handle errors inline meaning that it will display the line number of an error or when I call echo "text" it displays text in the correct position inside the template.
How do I create something along those lines?
http://www.phptal.org/ sounds very similar and has good code organization. if extension of mentioned system does not suit the needs, it would at least work as good tutorial
First of all: Immediately forget the idea about using a TE with XML-like tags. Really, it may look nice on the first glance but only causes too much work in the end and is really limiting.
Secondly I obviously recommend you to use Twig. It is clean, fast, extensible and offers all the features you need.
And lastly: I have written a small tutorial how to write a simple but powerful TE in another Stackoverflow question. It is really simple but for smaller projects it may suffice.
I cannot agree with NikiC's point of view.
XML is, although an old syntax, very powerful and brings a lot of advantages -- one of which is its similitude with properly written HTML.
There is nothing limiting in using an XML-based template syntax.
Besides, although Twig is, indeed, an excellent and famous project, it still lacks from a really good separation paradigm. It is still too dangerous and too easy to make mistakes from within the template and cause damages to the application as a whole.
Finally, the best template engine -- just as the best MVC framework -- is the one you feel really comfortable with.
I recommend having a look at FigDice]1, which was inspired by PHPTal, but takes things a few steps further, with an exclusive approach by giving the Web Designer (integrator, html-ist, etc.) a central position with the project -- much more flexible than the Twig-like approach.
I would be happy to read some feedback.
Thanks