Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
7 posts Page 1 of 1
Hello,

I accomplished a subtle change to how source output looks in browser's view-source console. It helps to quickly navigate ourselves along the lines.

So I spent a day to make it closer to expected with following source:
playground.zip
src
(901 Bytes) Downloaded 23 times


BEFORE:
2017-07-30-202423.png
2017-07-30-202423.png (26.16 KiB) Viewed 494 times


AFTER:
2017-07-31-122817.png
2017-07-31-122817.png (32.06 KiB) Viewed 494 times



Perhaps, if you see a way to tweak this a little more, I would certainly be interested in coding it a little further.

I added a line to /addons/kfunctions.php
Code: Select all
require_once( K_COUCH_DIR.'addons/custom-code/custom-events.php' );


Created folder 'custom-code' in couch/addons/ and placed a file 'custom-events.php' in it.
Content of custom-events.php:
Code: Select all
<?php

if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly

$FUNCS->add_event_listener( 'alter_str_to_parse', array('CustomEvents', 'tide') );


class CustomEvents{




    function tide(&$html){
        // https://www.couchcms.com/forum/viewtopic.php?f=8&t=10372#p26841

        /* this is a sample comment */

        /*
           this
           is
           a comment
        */

        /** I wrote this line of comment (since slash-star-star prefectly works even for a single line without ending symbols)
            and decided to expand it later, by adding a star-slash to the end of last string */
        $target_tags = 'pages|templates|extends|embed|block|block_parent|smart_embed|each|if';


        $what = array(
              '`\/\*\*.*\s`'                            // #1 /** comment
            , '`(?:\/\*[\s\S]*?\*\/)+?\s`'              // #2 /* comment */
            , '`.*\*\/\s`'                              // #3 comment */
            , '%<!--(?!<!)[^\[>].*?-->%'                // #4 html-comment // won't remove downlevel-revealed or downlevel-hidden [conditional comments].
            , '%^(\s*?<cms:.*\/>)$%m'                   // #5 Any self-closing. Add EOL after it closes the line and preserve spaces before it.
            , '%(<cms:.*?\/>\n)%m'                      // #5.1 Any self-closing. Add EOL after it closes the line.
            , '%^\s+(<cms:embed.*\/>)$%m'               // #6 A self-closing <cms:embed />. Remove spaces before it.
            , '%\n\s+<cms:(?=(' . $target_tags . '))%'  // #7 Remove spaces before selected tags
            , '%\s+<cms:(?=(' . $target_tags . '))%'    // #7.0 Remove spaces before selected tags even without newline before them
            , '%\n\s+</cms:(' . $target_tags . ')>%'    // #7.1 Remove spaces before closure of selected tags
            , '%\n\s+</cms:(' . $target_tags . ')>\n+%' // #7.2 Remove spaces before closure of selected tags and after it
            , '%(</cms:.*>)$%mu'                        // #8 Closure of tag should add EOL. Helpful for one-liners
            , '%\r\n\r\n%'                              // #9 Replace double newline to single
        );

        $with = array(
              ''                    // #1
            , ''                    // #2
            , ''                    // #3
            , ''                    // #4
            , '$1' . PHP_EOL        // #5
            , '$1' . PHP_EOL        // #5.1
            , PHP_EOL . '$1'        // #6
            , PHP_EOL . '<cms:'     // #7
            , PHP_EOL . '<cms:'     // #7.0
            , PHP_EOL . '</cms:$1>' // #7.1
            , PHP_EOL . '</cms:$1>' // #7.2
            , "\n" . '$1'           // #8
            , PHP_EOL               // #9
        );


        //error_log( print_r( 'BEFORE:' . PHP_EOL . $html, true ) );
        $html = preg_replace( $what, $with, $html);
        //error_log( print_r( 'AFTER:' . PHP_EOL . $html, true ) );
        return 1;


    }


}


The function above also has traces of my previous code that removes php-style and html-style comments. If you don't want it, it's easy to remove #1-4 elements from both arrays.
Thank you :)

Do not forget to comment. :!:
This is very interesting, I will look at it in great detail!
Good job
Code updated with a small fix.
Updated with keeping conditional html-comments as in
<!--[if IE 8]><html class="ie ie8"> <![endif]-->
<!--[if IE 9]><html class="ie ie9"> <![endif]-->

Added more tags: folders, extends, block, block_parent, smart_embed

I really like to see the code as beautiful as if no cms ever touched it :) Just geeky enough :mrgreen:
Fixed regex to better match start-end of line.
If there are no downsides to this and it just looks objectively better maybe its worth merging with the main CouchCMS GitHub
keakie wrote: If there are no downsides to this and it just looks objectively better maybe its worth merging with the main CouchCMS GitHub

Thanks, I'm still polishing it. Also, CouchCMS does its job by outputting the html mostly untouched, so stripping comments is not expected from it. It is for advanced scenarios and geeky coders who work with the source often. One downside is that this addon is always on (have to go to kfunctions to turn it off).
You are free to try it on your system as is.
7 posts Page 1 of 1

Who is online

In total there are 3 users online :: 0 registered, 0 hidden and 3 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 3 guests