├── CHANGELOG.md ├── README.md ├── kirbycms-extension-code-lib.php ├── kirbycms-extension-code.komodoproject └── kirbycms-extension-code.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # v1.5.2 2 | 3 | * Add plugin at the first position of the array `pre` and at the last position of array `post`. This is a workaround to make it possible to ignore the kirbytags and markdowns inner the code block. 4 | * Function to deactivate/activate parse content of code block with markdown and kirbytag 5 | 6 | # v1.5.1 7 | 8 | * Add option to activate and deactivate the parsing of the code content 9 | * Add some kirby config parameters to set some default values. 10 | 11 | # v1.5 12 | 13 | * Changed project structure for easy integrate into projects (git submodules) 14 | * Add Requirement `kirbycms-extension-webhelper` 15 | * Support for inline source code 16 | 17 | # v1.3 18 | 19 | * Change name from `Code` to `CodeExt` (also the class name is changed to `CodeExt`) 20 | * Code cleanup 21 | 22 | # v1.2 23 | 24 | * Add different CSS class to `figcaption` for caption on the top or bottom 25 | * Changed option `class` to `caption_class` 26 | 27 | # v1.1 28 | 29 | * Feature: Add namespace to the core class, for better difference with other classes and plugins 30 | * Feature: Add custom config parameter "kirby.extension.code.default_lang", default value is "false", possible values are "false" or a language string. 31 | * Feature: Add custom config parameter "kirby.extemsion.code.default_caption", default value is "false", possible values are "false", "{filebasename}", "{filename}", "{filedescription}" or a custom text 32 | 33 | # v1.0 34 | 35 | * Intial release with complete rebuild code -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KirbyText Extension - CodeExt 2 | 3 | Don't mess up your content files with huge code blocks! This plugin let's you specify code files which will get embedded into code blocks within your content when rendering your page. 4 | 5 | ## Requirements 6 | 7 | * [Kirby Extension - WebHelper](https://github.com/fanningert/kirbycms-extension-webhelper) 8 | 9 | ## Installation 10 | 11 | ### GIT 12 | 13 | Go into the `{kirby_installation}/site/plugins` directory and execute following command. 14 | 15 | ```bash 16 | $ git clone https://github.com/fanningert/kirbycms-extension-webhelper.git 17 | $ git clone https://github.com/fanningert/kirbycms-extension-code.git 18 | ``` 19 | 20 | ### GIT submodule 21 | 22 | Go in the root directory of your git repository and execute following command to add the repository as submodule to your GIT repository. 23 | 24 | ```bash 25 | $ git submodule add https://github.com/fanningert/kirbycms-extension-webhelper.git ./site/plugins/kirbycms-extension-webhelper 26 | $ git submodule add https://github.com/fanningert/kirbycms-extension-code.git ./site/plugins/kirbycms-extension-code 27 | $ git submodule init 28 | $ git submodule update 29 | ``` 30 | 31 | ### Manuell 32 | 33 | ## Update 34 | 35 | ### GIT 36 | 37 | Go into the `{kirby_installation}/site/plugins/kirbycms-extension-code` directory and execute following command. 38 | 39 | ```bash 40 | $ git pull 41 | ``` 42 | Don't forget to update the requirement `kirbycms-extension-webhelper`. 43 | 44 | ### GIT submodule 45 | 46 | Go in the root directory of your git repository and execute following command to update the submodule of this extension. 47 | 48 | ```bash 49 | $ git submodule update 50 | ``` 51 | 52 | ## Documentation 53 | 54 | ### Kirby configuration values 55 | 56 | | Kirby option | Default | Values | Description | 57 | | ------------ | ------- | ------ | ----------- | 58 | | `kirby.extension.codeext.lang` | false | false/{string} | Default code language | 59 | | `kirby.extension.codeext.caption_top` | true | true/false | Place the caption at the top of the code block | 60 | | `kirby.extension.codeext.caption_class` | 'code' | {string} | Class string for the figure element | 61 | | `kirby.extension.codeext.parse_content` | false | true/false | Parse the code with kirbytag and markdown | 62 | 63 | ### KirbyTag attributes 64 | 65 | | Option | Default | Values | Description | 66 | | ------ | ------- | ------ | ----------- | 67 | | lang | | false/{string} | see `kirby.extension.codeext.lang` | 68 | | caption | false | false/{string} | Caption of the code block | 69 | | caption_top | `none` | {string} | see `kirby.extension.codeext.caption_top` | 70 | | caption_class | | {string} | see `kirby.extension.codeext.caption_class` | 71 | | parse | | true/false | see `kirby.extension.codeext.parse_content` | 72 | 73 | ## Examples 74 | 75 | ### Code with language description 76 | 77 | ``` 78 | (code: code.html lang: html) 79 | ``` 80 | 81 | ### Code with caption 82 | 83 | ``` 84 | (code: code.html lang: html caption: HTML) 85 | ``` 86 | 87 | ### Code with included source code 88 | 89 | ``` 90 | (code lang: html caption: HTML) 91 | 92 | 93 | test 94 | 95 | 96 | Text 97 | 98 | 99 | (/code) 100 | ``` -------------------------------------------------------------------------------- /kirbycms-extension-code-lib.php: -------------------------------------------------------------------------------- 1 | page = $page; 31 | $this->loadDefaults(); 32 | } 33 | 34 | protected function loadDefaults(){ 35 | $this->default[self::ATTR_FILE] = false; 36 | $this->default[self::ATTR_LANG] = kirby()->option(self::CONFIG_LANG, false); 37 | $this->default[self::ATTR_CAPTION] = false; 38 | $this->default[self::ATTR_CAPTION_TOP] = kirby()->option(self::CONFIG_CAPTION_TOP, true); 39 | $this->default[self::ATTR_CAPTION_CLASS] = kirby()->option(self::CONFIG_CAPTION_CLASS, 'code-figure'); 40 | $this->default[self::ATTR_PARSE_CONTENT] = kirby()->option(self::CONFIG_PARSE_CONTENT, false); 41 | } 42 | 43 | public function getDefaults(){ 44 | return $this->default; 45 | } 46 | 47 | public function parseAndConvertTags($value, array $attr_template = null){ 48 | return $this->parseAndConvertTag(self::ATTR_FILE,$value, $attr_template); 49 | } 50 | 51 | protected function parseAndConvertTag($tag, $value, array $attr_template = null){ 52 | $offset = 0; 53 | while ( ($block = WebHelper::getblock($tag, $value, $offset)) !== false ) { 54 | $content = ""; 55 | $offset = $block[WebHelper::BLOCK_ARRAY_VALUE_ENDPOS]; 56 | $start = $block[WebHelper::BLOCK_ARRAY_VALUE_STARTPOS]; 57 | $length = $block[WebHelper::BLOCK_ARRAY_VALUE_ENDPOS]-$block[WebHelper::BLOCK_ARRAY_VALUE_STARTPOS]; 58 | $offset = $start + 1; 59 | 60 | $this->parse($tag, $block, $attr_template); 61 | if( $this->data[self::ATTR_PARSE_CONTENT] === true ){ 62 | $content = $this->toHTML(); 63 | } else { 64 | $content = "(".uniqid('code', true).")"; 65 | CodeExt::$replaceContent[$content] = $this->toHTML(); 66 | } 67 | 68 | $value = substr_replace($value, $content, $start, $length); 69 | $offset = $start + strlen($content); 70 | } 71 | 72 | return $value; 73 | } 74 | 75 | public function parse($tag, array $block, array $attr_template = null){ 76 | if ( is_array($block) && array_key_exists(WebHelper::BLOCK_ARRAY_VALUE_ATTRIBUTES, $block) ) 77 | $this->data = $this->convertAndMergeAttributes( $tag, $block[WebHelper::BLOCK_ARRAY_VALUE_ATTRIBUTES], $attr_template ); 78 | else 79 | $this->data = $this->convertAndMergeAttributes( $tag, null, $attr_template ); 80 | 81 | $this->content = $block[WebHelper::BLOCK_ARRAY_VALUE_CONTENT]; 82 | } 83 | 84 | protected function convertAndMergeAttributes($tag, array $attr = null, array $attr_template = null){ 85 | $attr_result = array(); 86 | $attr_result = $this->getDefaults(); 87 | 88 | if ( is_array($attr_template) ) { 89 | foreach ( $attr_template as $key => $value ) { 90 | if ( array_key_exists($key, $attr_result) ) 91 | $attr_result[$key] = $value; 92 | } 93 | } 94 | 95 | if ( is_array( $attr ) ) { 96 | foreach($attr as $key => $value){ 97 | if ( array_key_exists($key, $this->para_mapping) ) 98 | $key = $this->para_mapping[$key]; 99 | 100 | if ( array_key_exists($key, $attr_result) ) 101 | $attr_result[$key] = $this->checkValue( $key, $value ); 102 | } 103 | } 104 | 105 | return $attr_result; 106 | } 107 | 108 | protected function checkValue($key, $value){ 109 | switch($key){ 110 | case self::ATTR_PARSE_CONTENT: 111 | case self::ATTR_CAPTION_TOP: 112 | if ( is_bool($value) ) 113 | return; 114 | if ( is_string($value) ) 115 | $value = ( $value === "true" )? true : false; 116 | else 117 | $value = $this->default[self::ATTR_PARSE_CONTENT]; 118 | break; 119 | } 120 | return $value; 121 | } 122 | 123 | protected function convertContent($value){ 124 | $search_values = array("(", ")"); 125 | $replace_values = array("", ""); 126 | 127 | //$value = str_replace($search_values, $replace_values, $value); 128 | 129 | return $value; 130 | } 131 | 132 | public function toHTML(){ 133 | if ( !empty($this->data[self::ATTR_FILE]) ){ 134 | $source = $this->data[self::ATTR_FILE]; 135 | $source = ( is_object( $source ) )? $source : $this->page->file ( $source ); 136 | return self::getCodeBlockFromFile($source, 137 | $this->data[self::ATTR_LANG], 138 | $this->data[self::ATTR_CAPTION], 139 | $this->data[self::ATTR_CAPTION_TOP], 140 | $this->data[self::ATTR_CAPTION_CLASS]); 141 | } else { 142 | return self::getCodeBlock($this->content, 143 | $this->data[self::ATTR_LANG], 144 | $this->data[self::ATTR_CAPTION], 145 | $this->data[self::ATTR_CAPTION_TOP], 146 | $this->data[self::ATTR_CAPTION_CLASS]); 147 | } 148 | } 149 | /** 150 | * 151 | * @param mixed $file 152 | * @param string $lang 153 | * @param string $caption 154 | * @param string $caption_top 155 | * @param string $caption_class 156 | * @return string 157 | */ 158 | public static function getCodeBlockFromFile($file, $lang = false, $caption = false, $caption_top = true, $caption_class = false) { 159 | if ($file && is_object($file)) 160 | $code = $file->read (); 161 | else 162 | $code = 'Unknown content file'; 163 | return ( string ) self::getCodeBlock ( $code, $lang, $caption, $caption_top, $caption_class ); 164 | } 165 | 166 | /** 167 | * 168 | * @param string $code 169 | * @param string $lang 170 | * @param string $caption 171 | * @param string $caption_top 172 | * @param string $caption_class 173 | * @return string 174 | */ 175 | public static function getCodeBlock($code, $lang = false, $caption = false, $caption_top = true, $caption_class = false) { 176 | $code = WebHelper::convert ( $code ); 177 | 178 | $attr = array(); 179 | if ( $lang !== false ) { 180 | $attr['class'] = "language-" . $lang; 181 | $attr['data-lang'] = $lang; 182 | } 183 | $code_block = \Html::tag("code", $code, $attr); 184 | 185 | $attr = array(); 186 | $attr['class'] = "highlight"; 187 | $pre_block = \Html::tag("pre", $code_block, $attr); 188 | 189 | //Figure 190 | return WebHelper::blockFigure($pre_block, $caption, $caption_top, $caption_class); 191 | } 192 | 193 | } -------------------------------------------------------------------------------- /kirbycms-extension-code.komodoproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | PHP 14 | 15 | 16 | 17 | application/x-www-form-urlencoded 18 | GET 19 | 1 20 | 0 21 | 0 22 | 23 | 24 | 25 | 26 | PHP 27 | PHP 28 | Project 29 | 1 30 | 31 | 32 | -------------------------------------------------------------------------------- /kirbycms-extension-code.php: -------------------------------------------------------------------------------- 1 | field->page); 10 | return $codeExt->parseAndConvertTags($value); 11 | }; 12 | 13 | $codeFunctionPost = function($kirbytext, $value) { 14 | $search = array_keys(CodeExt::$replaceContent); 15 | $replace = array_values(CodeExt::$replaceContent); 16 | if( count($search) > 0 ){ 17 | $value = str_replace($search, $replace, $value); 18 | } 19 | 20 | return $value; 21 | }; 22 | 23 | array_unshift(kirbytext::$pre, $codeFunctionPre); 24 | kirbytext::$post[999] = $codeFunctionPost; --------------------------------------------------------------------------------