Textpattern | PHP Cross Reference | Content Management Systems |
Description: Textpack parser.
1 <?php 2 3 /* 4 * Textpattern Content Management System 5 * http://textpattern.com 6 * 7 * Copyright (C) 2016 The Textpattern Development Team 8 * 9 * This file is part of Textpattern. 10 * 11 * Textpattern is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation, version 2. 14 * 15 * Textpattern is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with Textpattern. If not, see <http://www.gnu.org/licenses/>. 22 */ 23 24 /** 25 * Textpack parser. 26 * 27 * @since 4.6.0 28 * @package Textpack 29 */ 30 31 namespace Textpattern\Textpack; 32 33 class Parser 34 { 35 /** 36 * Stores the default language. 37 * 38 * @var string 39 */ 40 41 protected $language; 42 43 /** 44 * Stores the default owner. 45 * 46 * @var string 47 */ 48 49 protected $owner; 50 51 /** 52 * Constructor. 53 */ 54 55 public function __construct() 56 { 57 $this->language = get_pref('language', 'en-gb'); 58 $this->owner = TEXTPATTERN_LANG_OWNER_SITE; 59 } 60 61 /** 62 * Sets the default language. 63 * 64 * @param string $language The language code 65 */ 66 67 public function setLanguage($language) 68 { 69 $this->language = $language; 70 } 71 72 /** 73 * Sets the default owner. 74 * 75 * @param string $owner The default owner 76 */ 77 78 public function setOwner($owner) 79 { 80 $this->owner = $owner; 81 } 82 83 /** 84 * Converts a Textpack to an array. 85 * 86 * <code> 87 * $textpack = \Textpattern\Textpack\Parser(); 88 * print_r( 89 * $textpack->parse("string => translation") 90 * ); 91 * </code> 92 * 93 * @param string $textpack The Textpack 94 * @return array An array of translations 95 */ 96 97 public function parse($textpack) 98 { 99 $lines = explode(n, (string)$textpack); 100 $out = array(); 101 $version = false; 102 $lastmod = false; 103 $event = false; 104 $language = $this->language; 105 $owner = $this->owner; 106 107 foreach ($lines as $line) { 108 $line = trim($line); 109 110 // A comment line. 111 if (preg_match('/^#[^@]/', $line, $m)) { 112 continue; 113 } 114 115 // Sets version and lastmod timestamp. 116 if (preg_match('/^#@version\s+([^;\n]+);?([0-9]*)$/', $line, $m)) { 117 $version = $m[1]; 118 $lastmod = $m[2] !== false ? $m[2] : $lastmod; 119 continue; 120 } 121 122 // Sets language. 123 if (preg_match('/^#@language\s+(.+)$/', $line, $m)) { 124 $language = $m[1]; 125 continue; 126 } 127 128 // Sets owner. 129 if (preg_match('/^#@owner\s+(.+)$/', $line, $m)) { 130 $owner = $m[1]; 131 continue; 132 } 133 134 // Sets event. 135 if (preg_match('/^#@([a-zA-Z0-9_-]+)$/', $line, $m)) { 136 $event = $m[1]; 137 continue; 138 } 139 140 // Translation. 141 if (preg_match('/^([\w\-]+)\s*=>\s*(.+)$/', $line, $m)) { 142 if (!empty($m[1]) && !empty($m[2])) { 143 $out[] = array( 144 'name' => $m[1], 145 'lang' => $language, 146 'data' => $m[2], 147 'event' => $event, 148 'owner' => $owner, 149 'version' => $version, 150 'lastmod' => $lastmod, 151 ); 152 } 153 } 154 } 155 156 return $out; 157 } 158 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title