WordPress.org

Plugin Directory

Changeset 482884


Ignore:
Timestamp:
01/01/12 00:25:50 (2 years ago)
Author:
mattwiebe
Message:

Let's go 2.0. Including:

  • Move to opt-out model
  • Add experiemental HTML to Markdown conversion using Markdownify
Location:
markdown-on-save-improved/trunk
Files:
7 added
3 edited

Legend:

Unmodified
Added
Removed
  • markdown-on-save-improved/trunk/markdown-on-save.php

    r464303 r482884  
    22/* 
    33Plugin Name: Markdown on Save Improved 
    4 Description: Allows you to compose content in Markdown on a per-item basis from WP admin or mobile/3rd-party apps. The markdown version is stored separately, so you can deactivate this plugin and your posts won't spew out Markdown. Based on <a href="http://wordpress.org/extend/plugins/markdown-on-save/">Mark Jaquith's plugin</a>. 
    5 Version: 1.0.2 
     4Description: Allows you to compose content in Markdown on a per-item basis from WP admin or mobile/3rd-party apps. The markdown version is stored separately, so you can deactivate this plugin and your posts won't spew out Markdown. Based on <a href="http://wordpress.org/extend/plugins/markdown-osi/">Mark Jaquith's plugin</a>. 
     5Version: 2.0 
    66Author: Matt Wiebe 
    77Author URI: http://somadesign.ca/ 
     8License: GPL v2 
    89*/ 
    910 
     11/* 
     12 * Copyright 2011 Matt Wiebe. 
     13 * 
     14 * This software is forked from the original Markdown on Save plugin (c) Mark Jaquith 
     15 * It uses the Markdown Extra and Markdownify libraries. Copyrights and licenses indicated in said libararies. 
     16 * 
     17 */ 
     18 
    1019class SD_Markdown { 
    11     const PM = '_cws_is_markdown'; 
     20    const PM = '_sd_disable_markdown'; 
     21    const CONVERT = 'sd_convert_to_markdown'; 
    1222    var $instance; 
    1323 
     
    1828 
    1929    public function init() { 
    20         load_plugin_textdomain( 'markdown-on-save', NULL, basename( dirname( __FILE__ ) ) ); 
     30        load_plugin_textdomain( 'markdown-osi', NULL, basename( dirname( __FILE__ ) ) ); 
    2131        add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); 
    2232        add_action( 'do_meta_boxes', array( $this, 'do_meta_boxes' ), 20, 2 ); 
     
    4252        if ( $this->is_markdown($post_id) ) { 
    4353            $post = get_post($post_id); 
    44             $post->post_content = $post->post_content_filtered; 
     54            if ( ! empty($post->post_content_filtered) ) 
     55                $post->post_content = $post->post_content_filtered; 
    4556            wp_cache_delete($post->ID, 'posts'); 
    4657            wp_cache_add($post->ID, $post, 'posts'); 
     
    6576        // run once 
    6677        remove_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); 
    67         $box_ticked = ( isset( $_POST['cws_using_markdown'] ) && wp_verify_nonce( $_POST['_cws_markdown_nonce'], 'cws-markdown-save' ) ); 
    68         $md_comment_inserted = ( false !== stripos($data['post_content'], '<!--markdown-->') ); 
    69         if ( $box_ticked || $md_comment_inserted ) { 
    70             $id = $postarr['ID']; 
     78         
     79        // checks 
     80        $nonced = ( isset( $_POST['_sd_markdown_nonce'] ) && wp_verify_nonce( $_POST['_sd_markdown_nonce'], 'sd-markdown-save' ) ); 
     81        $disable_ticked = ( $nonced && isset( $_POST['sd_disable_markdown'] ) ); 
     82        $disable_comment_inserted = ( false !== stripos($data['post_content'], '<!--no-markdown-->') ); 
     83        $do_html_to_markdown = ( $nonced && isset($_POST[self::CONVERT]) ); 
     84        $id = ( isset( $postarr['ID']) ) ? $postarr['ID'] : 0; 
     85         
     86        // Maybe do HTML → Markdown 
     87        if ( $do_html_to_markdown )  
     88            $data['post_content'] = $this->html_to_markdown($data['post_content']); 
     89         
     90        // Make sure markdown processing isn't disabled for this post 
     91        if ( ! ( $disable_ticked || $disable_comment_inserted ) ) { 
    7192            $data['post_content_filtered'] = $data['post_content']; 
    7293            // Do markdown processing 
    7394            $data['post_content'] = $this->process($data['post_content'], $id); 
    74             if ( $postarr['ID'] ) 
    75                 update_post_meta( $postarr['ID'], self::PM, true ); 
     95            if ( $id ) 
     96                delete_post_meta( $id, self::PM ); 
    7697        } else { 
    7798            $data['post_content_filtered'] = ''; 
    78             if ( $postarr['ID'] ) 
    79                 delete_post_meta( $postarr['ID'], self::PM ); 
     99            if ( $id ) 
     100                update_post_meta( $id, self::PM, true ); 
    80101        } 
    81102        return $data; 
    82103    } 
    83104     
     105    protected function html_to_markdown($content) { 
     106        $content = stripslashes($content); 
     107        $content = wpautop($content); 
     108        require_once( dirname(__FILE__) . '/markdownify/markdownify_extra.php' ); 
     109        $md = new Markdownify_Extra(true); 
     110        $content = $md->parseString($content); 
     111        return $content; 
     112    } 
     113     
    84114    protected function process($content, $id) { 
    85115        require_once( dirname( __FILE__) . '/markdown-extra/markdown-extra.php' ); 
    86         // leaving the comment in produces odd Markdown sometimes 
    87         $content = str_ireplace('<!--markdown-->', '', $content); 
    88116        // Links with "quoted titles" bork without stripping slashes. 
    89117        // Stripslashes would lose desired backslashes. So, regex. 
     
    97125 
    98126    public function do_meta_boxes( $type, $context ) { 
     127        // allow disabling for folks who think markdown should always be on. 
     128        if ( defined( 'SD_HIDE_MARKDOWN_BOX') && SD_HIDE_MARKDOWN_BOX ) 
     129            return; 
     130 
    99131        if ( 'side' == $context && in_array( $type, array_keys( get_post_types() ) ) ) 
    100             add_meta_box( 'cws-markdown', __( 'Markdown', 'markdown-on-save' ), array( $this, 'meta_box' ), $type, 'side', 'high' ); 
     132            add_meta_box( 'sd-markdown', __( 'Markdown', 'markdown-osi' ), array( $this, 'meta_box' ), $type, 'side', 'high' ); 
    101133    } 
    102134 
    103135    public function meta_box() { 
    104136        global $post; 
    105         wp_nonce_field( 'cws-markdown-save', '_cws_markdown_nonce', false, true ); 
    106         echo '<p><input type="checkbox" name="cws_using_markdown" id="cws_using_markdown" value="1" '; 
     137        wp_nonce_field( 'sd-markdown-save', '_sd_markdown_nonce', false, true ); 
     138        echo '<p><input type="checkbox" name="sd_disable_markdown" id="sd_disable_markdown" value="1" '; 
    107139        checked( !! get_post_meta( $post->ID, self::PM, true ) ); 
    108         echo ' /> <label for="cws_using_markdown">' . __( 'This post is formatted with Markdown', 'markdown-on-save' ) . '</label></p>'; 
     140        echo ' /> <label for="sd_disable_markdown">' . __( 'Disable Markdown formatting', 'markdown-osi' ) . '</label></p>'; 
     141        printf( '<p><label><input type="checkbox" name="%s" /> %s</label></p>', self::CONVERT, __('Convert HTML to Markdown (experimental)', 'markdown-osi') ); 
    109142    } 
    110143 
     
    114147 
    115148    private function is_markdown( $id ) { 
    116         return !! get_post_meta( $id, self::PM, true ); 
     149        return ! get_post_meta( $id, self::PM, true ); 
    117150    } 
    118151 
     
    120153        if ( $this->is_markdown( $id ) ) { 
    121154            $post = get_post( $id ); 
    122             if ( $post ) 
     155            if ( $post && ! empty( $post->post_content_filtered ) ) 
    123156                $content = $post->post_content_filtered; 
    124157        } 
  • markdown-on-save-improved/trunk/readme.txt

    r464303 r482884  
    22Contributors: mattwiebe 
    33Tags: markdown, formatting, mobile 
    4 Requires at least: 3.0 
    5 Tested up to: 3.2.1 
    6 Stable tag: 1.0.2 
     4Requires at least: 3.2 
     5Tested up to: 3.3 
     6Stable tag: 2.0 
    77 
    8 Allows you to compose content in Markdown on a per-item basis. The markdown version is stored separately, so you can deactivate this plugin and your posts won't spew out Markdown. 
     8Markdown + WordPress = writing bliss. The markdown version is stored separately, so you can deactivate this plugin and your posts won't spew out Markdown. 
    99 
    1010== Description == 
    1111 
    12 The improved version allows you to insert a `<!--markdown-->` comment from whichever mobile app or 3rd party blogging application you use to designate a post as formatted for Markdown. 
     12WordPress will process your post with Markdown unless you tell it not to. You probably won't want to, since writing in Markdown is so awesome. Compatible with mobile apps and 3rd-party blogging tools. 
    1313 
    14 This plugin allows you to compose content in Markdown on a per-item basis. The markdown version is stored separately (in the `post_content_formatted` column), so you can deactivate this plugin and your posts won't spew out Markdown, because HTML is stored in the `post_content`, just like normal. This is also much faster than doing on-the-fly Markdown conversion on every page load. It's only done once! When you re-edit the post, the markdown version is swapped into the editor for you to edit. If something external updates the post content, you'll lose the Markdown version. 
     14The main difference between this plugin and the [original](http://wordpress.org/extend/plugins/markdown-on-save/) is that this plugin assumes you always want Markdown processing. 
     15 
     16The markdown version is stored separately (in the `post_content_formatted` column), so you can deactivate this plugin and your posts won't spew out Markdown, because HTML is stored in the `post_content` column, just like normal. This is also much faster than doing on-the-fly Markdown conversion on every page load. It's only done once! When you re-edit the post, the markdown version is swapped into the editor for you to edit. 
    1517 
    1618== Installation == 
     
    20222. Activate the "Markdown On Save Improved" plugin in your WordPress administration interface 
    2123 
    22 3. Create a new post with Markdown, and check the "This post is formatted with Markdown" box, or put a `<!--markdown-->` HTML comment somewhere in the body of your post. 
     243. Create a new post with Markdown. 
    2325 
    24 4. Done! Now that post can be edited using Markdown, but will save as processed HTML on the backend. 
     264. Your post can be edited using Markdown, but will save as processed HTML on the backend. 
    2527 
    2628== Screenshots == 
    2729 
    28 1. The meta box where you designate a post as containing Markdown. This is the only UI for the plugin! 
     301. The meta box where you can disable Markdown formatting or convert Markdown to HTML. 
    2931 
    3032== Frequently Asked Questions == 
     
    3436Please refer to this resource: [http://michelf.com/projects/php-markdown/extra/](PHP Markdown Extra). 
    3537 
    36 = How do I enable Markdown mode from a mobile app or 3rd party client such as Mars Edit? = 
     38= How do I disable Markdown processing? = 
    3739 
    38 Simply put a `<!--markdown-->` HTML comment anywhere in your post body. If you delete it, it's the equivalent of unchecking the Markdown box. 
     40In most cases this should be unnecessary, since Markdown ignores existing HTML. But if Markdown for some reason disturbs your HTML when making an edit to a non-Markdown post, either check the "Disable Markdown formatting" checkbox, or put a `<!--no-markdown-->` HTML comment in your post somewhere (useful when using a mobile or 3rd party blogging app). 
    3941 
    40 = What happens if I uncheck the Markdown box? = 
     42= How do I convert an existing post to Markdown? = 
    4143 
    42 Your post will no longer be interpreted as Markdown, and you may have to alter the post to remove Markdown formatting. 
     44There is an experimental checkbox in the post editor to convert your old HTML post to Markdown using [Markdownify][]. **Check it at your own risk.** Make sure you have revisions on or backups. If you're relying on specifically crafted HTML, it might get destroyed. But it might be cool. 
     45 
     46[Markdownify]: http://milianw.de/projects/markdownify/ 
     47 
     48= What if I don't want to see that Markdown meta box at all? = 
     49 
     50I see you love Markdown greatly. You have chosen wisely. Add the following constant to your wp-config.php file: 
     51 
     52    define( 'SD_HIDE_MARKDOWN_BOX', true ); 
     53 
     54Note that you can still disable Markdown formatting with a `<!--no-markdown-->` HTML comment. HTML to Markdown conversion will be impossible. 
    4355 
    4456= What happens if I decide I don't want this plugin anymore? = 
     
    4860== Changelog == 
    4961 
     62= 2.0 = 
     63 
     64* Move to opt-out model 
     65* Add experiemental HTML to Markdown conversion using [Markdownify][] 
     66 
    5067= 1.0.2 = 
     68 
    5169* Fix bug where desired backslashes went missing 
    5270 
    5371= 1.0.1 = 
     72 
    5473* Add stripslashes to let Markdown links with titles get parsed. 
    5574 
    5675= 1.0 = 
     76 
    5777* Initial forked release 
    5878 
Note: See TracChangeset for help on using the changeset viewer.