Skip to content

Instantly share code, notes, and snippets.

@jonom
Last active December 10, 2015 20:38
Show Gist options
  • Select an option

  • Save jonom/cd4916450ddc19e25117 to your computer and use it in GitHub Desktop.

Select an option

Save jonom/cd4916450ddc19e25117 to your computer and use it in GitHub Desktop.

Revisions

  1. jonom revised this gist Dec 10, 2015. 1 changed file with 18 additions and 3 deletions.
    21 changes: 18 additions & 3 deletions TextRaw2P.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    /**
    * SilverStripe TextRaw2P extension.
    * TextRaw2P class.
    * Apply this extension to the Text class to access $YourText.Raw2P in templates.
    *
    */
    @@ -12,14 +12,29 @@ class TextRaw2P extends DataExtension {
    );

    /**
    * Convert raw text to html and replace double line breaks with paragraphs
    * Convert raw text to html and replace double line breaks with paragraphs.
    *
    * Before:
    * -------
    * Hey this is a paragraph.
    *
    * And this is another one.
    * With a manual line break.
    * -------
    *
    * After:
    * -------
    * <p>Hey this is a paragraph.</p>
    * <p>And this is another one.<br>
    * With a manual line break.</p>
    * -------
    *
    * @return string
    */
    public function Raw2P() {
    $xml = Convert::RAW2XML(trim($this->owner->value));
    $xml = nl2br($xml);
    return '<p>' . preg_replace('#(<br />[\r\n]+){2}#', '</p><p>', $xml) . '</p>';
    return '<p>' . preg_replace('#(<br />[\r\n]+){2}#', "</p>\r\n<p>", $xml) . '</p>';
    }

    }
  2. jonom revised this gist Dec 10, 2015. No changes.
  3. jonom created this gist Dec 10, 2015.
    25 changes: 25 additions & 0 deletions TextRaw2P.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?php

    /**
    * SilverStripe TextRaw2P extension.
    * Apply this extension to the Text class to access $YourText.Raw2P in templates.
    *
    */
    class TextRaw2P extends DataExtension {

    private static $casting = array(
    'Raw2P' => 'HTMLText'
    );

    /**
    * Convert raw text to html and replace double line breaks with paragraphs
    *
    * @return string
    */
    public function Raw2P() {
    $xml = Convert::RAW2XML(trim($this->owner->value));
    $xml = nl2br($xml);
    return '<p>' . preg_replace('#(<br />[\r\n]+){2}#', '</p><p>', $xml) . '</p>';
    }

    }