PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/generate-trstrings.php

https://github.com/phalcon/docs
PHP | 313 lines | 252 code | 59 blank | 2 comment | 51 complexity | f7ebf5966e33b6fe9520acaec00da89c MD5 | raw file
Possible License(s): CC-BY-3.0
  1. <?php
  2. class Docs
  3. {
  4. private $_output = '';
  5. private $_prefix;
  6. private $_uniqueStrings = array();
  7. public function processUniqueSection($section, &$uniqueStrings)
  8. {
  9. if (!isset($section[0])) {
  10. return array();
  11. }
  12. $first = true;
  13. $newSection = array();
  14. foreach ($section as $line) {
  15. if ($first) {
  16. if (!trim($line)) {
  17. continue;
  18. }
  19. $first = false;
  20. $newSection[] = $line;
  21. } else {
  22. $newSection[] = $line;
  23. }
  24. }
  25. $section = $newSection;
  26. if (preg_match('#[ \t]*\.\.[ \t]+code-block::#', $section[0])) {
  27. foreach ($section as $str) {
  28. if (preg_match('#//[ \t]*([^;]*)[\r\n]$#', $str, $matches)) {
  29. if (preg_match('#[a-zA-Z]#', $str)) {
  30. $key = $this->_prefix . '_' . md5($matches[1]);
  31. $str = str_replace($matches[1], '{%' . $key . '%}', $str);
  32. $this->_uniqueStrings[$key] = $matches[1];
  33. }
  34. }
  35. $uniqueStrings[] = array('type' => 'code-section', 'value' => $str);
  36. }
  37. } else {
  38. if (substr($section[0], 0, 7) == '+------' || preg_match('#[ \t]*\.\.[ \t]+[a-z\-]+::#', $section[0])) {
  39. $section1 = join('', $section);
  40. $uniqueStrings[] = array('type' => 'text-raw', 'value' => $section1);
  41. $uniqueStrings[] = array('type' => 'separator', 'value' => PHP_EOL);
  42. } else {
  43. $list = true;
  44. foreach ($section as $position => $line) {
  45. if (!preg_match('#^[ \t]*\* #', $line)) {
  46. $list = false;
  47. break;
  48. }
  49. }
  50. if (!$list) {
  51. $markdownLinks = true;
  52. foreach ($section as $position => $line) {
  53. if (!preg_match('#^\.\.[ \t]+\_[a-zA-Z]#', $line)) {
  54. $markdownLinks = false;
  55. break;
  56. }
  57. }
  58. } else {
  59. $markdownLinks = false;
  60. }
  61. if ($list) {
  62. foreach ($section as $position => $line) {
  63. if (preg_match('#^[ \t]*\* (.*)#', $line, $listMatches)) {
  64. $number = 1;
  65. $placeholders = array();
  66. if (preg_match_all('#:doc:`[^`]+`#', $line, $matches, PREG_SET_ORDER)) {
  67. foreach ($matches as $position => $match) {
  68. $placeholders[$position] = $match[0];
  69. $listMatches[1] = str_replace($match[0], ':' . ($number) . ':', $listMatches[1]);
  70. $number++;
  71. }
  72. }
  73. if (preg_match_all('#`[^`]+`_#', $line, $matches, PREG_SET_ORDER)) {
  74. foreach ($matches as $position => $match) {
  75. $placeholders[$position] = $match[0];
  76. $listMatches[1] = str_replace($match[0], ':' . ($number) . ':', $listMatches[1]);
  77. $number++;
  78. }
  79. }
  80. if (preg_match_all('#[a-zA-Z0-9]+_ #', $line, $matches, PREG_SET_ORDER)) {
  81. foreach ($matches as $position => $match) {
  82. $placeholders[$position] = $match[0];
  83. $listMatches[1] = str_replace($match[0], ':' . ($number) . ': ', $listMatches[1]);
  84. $number++;
  85. }
  86. }
  87. if (!preg_match('#^:([0-9]+):$#', $listMatches[1])) {
  88. $key = $this->_prefix . '_' . md5($listMatches[0]);
  89. if (count($placeholders)) {
  90. $section[$position] = str_replace($listMatches[1], '{%' . $key . '|' . join('|', $placeholders) . '%}', $line);
  91. } else {
  92. $section[$position] = str_replace($listMatches[1], '{%' . $key . '%}', $line);
  93. }
  94. $this->_uniqueStrings[$key] = $listMatches[1];
  95. }
  96. }
  97. }
  98. }
  99. if (!$list && !$markdownLinks) {
  100. $section1 = str_replace(array("\r\n", "\n"), ' ', join('', $section));
  101. $originalSection1 = $section1;
  102. $number = 1;
  103. $placeholders = array();
  104. if (preg_match_all('#:doc:`[^`]+`#', $section1, $matches, PREG_SET_ORDER)) {
  105. foreach ($matches as $position => $match) {
  106. $placeholders[$position] = $match[0];
  107. $section1 = str_replace($match[0], ':' . ($number) . ':', $section1);
  108. $number++;
  109. }
  110. }
  111. if (preg_match_all('#`[^`]+`_#', $section1, $matches, PREG_SET_ORDER)) {
  112. foreach ($matches as $position => $match) {
  113. $placeholders[$position] = $match[0];
  114. $section1 = str_replace($match[0], ':' . ($number) . ':', $section1);
  115. $number++;
  116. }
  117. }
  118. if (preg_match_all('#[a-zA-Z0-9]+_ #', $section1, $matches, PREG_SET_ORDER)) {
  119. foreach ($matches as $position => $match) {
  120. $placeholders[$position] = $match[0];
  121. $section1 = str_replace($match[0], ':' . ($number) . ': ', $section1);
  122. $number++;
  123. }
  124. }
  125. $hash1 = md5(mb_strtolower($originalSection1));
  126. $uniqueStrings[] = array('type' => 'text-section', 'consecutive' => $hash1, 'value' => $section1, 'placeholders' => $placeholders);
  127. } else {
  128. $section1 = join('', $section);
  129. $uniqueStrings[] = array('type' => 'text-raw', 'value' => $section1);
  130. }
  131. }
  132. }
  133. }
  134. public function processSection($section)
  135. {
  136. if (!isset($section[0])) {
  137. return array();
  138. }
  139. $first = true;
  140. $newSection = array();
  141. foreach ($section as $line) {
  142. if ($first) {
  143. if (!trim($line)) {
  144. continue;
  145. }
  146. $first = false;
  147. $newSection[] = $line;
  148. } else {
  149. $newSection[] = $line;
  150. }
  151. }
  152. $section = $newSection;
  153. $separator = null;
  154. $twoSections = false;
  155. foreach ($section as $number => $line) {
  156. if (preg_match('!^[\-=\~#\^\*]{2,}$!', trim($line))) {
  157. $separator = $line;
  158. $twoSections = $number;
  159. break;
  160. }
  161. }
  162. if ($twoSections !== false) {
  163. $section1parts = array_slice($section, 0, $number);
  164. $section2parts = array_slice($section, $number + 1);
  165. $uniqueStrings = array();
  166. $this->processUniqueSection($section1parts, $uniqueStrings);
  167. $uniqueStrings[] = array('type' => 'separator', 'value' => $separator);
  168. $this->processUniqueSection($section2parts, $uniqueStrings);
  169. } else {
  170. $uniqueStrings = array();
  171. $this->processUniqueSection($section, $uniqueStrings);
  172. }
  173. $uniqueStrings[] = array('type' => 'separator', 'value' => PHP_EOL);
  174. return $uniqueStrings;
  175. }
  176. public function outputStrings($uniqueStrings)
  177. {
  178. foreach ($uniqueStrings as $consecutive => $uniqueString) {
  179. switch ($uniqueString['type']) {
  180. case 'text-section':
  181. $key = $this->_prefix . '_' . $uniqueString['consecutive'];
  182. if (count($uniqueString['placeholders'])) {
  183. $this->_output .= '%{' . $key . '|' . join('|', $uniqueString['placeholders']) . '}%' . PHP_EOL;
  184. } else {
  185. $this->_output .= '%{' . $key . '}%' . PHP_EOL;
  186. }
  187. $this->_uniqueStrings[$key] = rtrim($uniqueString['value']);
  188. break;
  189. case 'code-section':
  190. case 'text-raw':
  191. $this->_output .= $uniqueString['value'];
  192. break;
  193. case 'separator':
  194. $this->_output .= $uniqueString['value'];
  195. break;
  196. }
  197. }
  198. }
  199. public function processFile($path)
  200. {
  201. $section = array();
  202. $block = false;
  203. $this->_output = '';
  204. //$this->_uniqueStrings = array();
  205. foreach (file($path) as $line) {
  206. if (!trim($line) && !$block) {
  207. $uniqueStrings = $this->processSection($section);
  208. $this->outputStrings($uniqueStrings);
  209. $section = array();
  210. } else {
  211. if (!$block) {
  212. if (preg_match('#[ \t]*\.\.[ \t]+[a-z\-]+::#', $line)) {
  213. $block = true;
  214. }
  215. } else {
  216. if (preg_match('/^[A-Za-z]/', $line)) {
  217. $block = false;
  218. $uniqueStrings = $this->processSection($section);
  219. $this->outputStrings($uniqueStrings);
  220. $section = array();
  221. }
  222. }
  223. $section[] = $line;
  224. }
  225. }
  226. }
  227. public function processBaseDirectory($directory)
  228. {
  229. $recursiveDirectoryIterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS);
  230. /** @var $iterator RecursiveDirectoryIterator[] */
  231. $iterator = new RecursiveIteratorIterator($recursiveDirectoryIterator);
  232. foreach ($iterator as $item) {
  233. if ($item->getExtension() == 'rst') {
  234. $path = $item->getPathname();
  235. if (strpos($path, '_build/') === false && strpos($path, 'api/') === false) {
  236. $this->_prefix = str_replace('.rst', '', basename($path));
  237. $this->processFile($path);
  238. $baseRstPath = 'transifex/base-rst/' . dirname($path);
  239. @mkdir($baseRstPath, 0777, true);
  240. file_put_contents($baseRstPath . '/' . $this->_prefix . '.rst', $this->_output);
  241. }
  242. }
  243. }
  244. $baseStrPath = 'transifex/strings/';
  245. @mkdir($baseStrPath, 0777, true);
  246. file_put_contents($baseStrPath . '/en.json', json_encode($this->_uniqueStrings, JSON_PRETTY_PRINT));
  247. }
  248. public function run()
  249. {
  250. $this->processBaseDirectory('en');
  251. }
  252. }
  253. $d = new Docs();
  254. $d->run();