Skip to content

Instantly share code, notes, and snippets.

@daltonrooney
Last active May 22, 2020 17:15
Show Gist options
  • Select an option

  • Save daltonrooney/ab170a14e938a29562d834004ca08436 to your computer and use it in GitHub Desktop.

Select an option

Save daltonrooney/ab170a14e938a29562d834004ca08436 to your computer and use it in GitHub Desktop.

Revisions

  1. daltonrooney revised this gist May 22, 2020. 1 changed file with 21 additions and 2 deletions.
    23 changes: 21 additions & 2 deletions twig-filters.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,29 @@
    <?php

    // Requires WP-Markdown
    // Usage {{post.title|md2html}} {{post.description|html2md}}
    add_filter( 'timber/twig', 'add_to_twig' );

    function add_to_twig( $twig ) {
    $twig->addFilter( new Timber\Twig_Filter( 'noWidows', 'twigNoWidow' ) );
    return $twig;
    }

    function twigNoWidow($text = "", $numberOfWords = 1, $outputRaw = true ) {
    $tags = "a|span|i|b|em|strong|acronym|caps|sub|sup|abbr|big|small|code|cite|tt";

    // Taken from https://github.com/davethegr8/cakephp-typogrify-helper/blob/master/views/helpers/typogrify.php
    $regex = "/([^\s])\s+(((<($tags)[^>]*>)*\s*[^\s<>]+)(<\/($tags)>)*[^\s<>]*\s*(<\/(p|h[1-6]|li)>|$))/i";
    $string = $text;

    for ($i = 0; $i < $numberOfWords; $i++) {
    $string = preg_replace($regex, '$1&nbsp;$2', $string);
    }
    return $string;
    }

    if ( function_exists('wpmarkdown_markdown_to_html') && function_exists('wpmarkdown_html_to_markdown') ) {
    add_filter( 'timber/twig', 'md_add_to_twig' );
    // Requires WP-Markdown
    // Usage {{post.title|md2html}} {{post.description|html2md}}

    function md_add_to_twig( $twig ) {
    $twig->addFilter( new Timber\Twig_Filter( 'md2html', 'md2html' ) );
  2. daltonrooney revised this gist May 8, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion twig-filters.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    // Requires WP-Markdown Example {{post.title|md2html}} {{post.description|html2md}}
    <?php

    // Requires WP-Markdown
    // Usage {{post.title|md2html}} {{post.description|html2md}}

    if ( function_exists('wpmarkdown_markdown_to_html') && function_exists('wpmarkdown_html_to_markdown') ) {
    add_filter( 'timber/twig', 'md_add_to_twig' );
  3. daltonrooney created this gist May 8, 2020.
    19 changes: 19 additions & 0 deletions twig-filters.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // Requires WP-Markdown Example {{post.title|md2html}} {{post.description|html2md}}

    if ( function_exists('wpmarkdown_markdown_to_html') && function_exists('wpmarkdown_html_to_markdown') ) {
    add_filter( 'timber/twig', 'md_add_to_twig' );

    function md_add_to_twig( $twig ) {
    $twig->addFilter( new Timber\Twig_Filter( 'md2html', 'md2html' ) );
    $twig->addFilter( new Timber\Twig_Filter( 'html2md', 'html2md' ) );
    return $twig;
    }

    function md2html($content) {
    return wpmarkdown_markdown_to_html($content);
    }

    function html2md($content) {
    return wpmarkdown_html_to_markdown($content);
    }
    }