htm.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * The text element class file of zin of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
  6. * @author Hao Sun <sunhao@easycorp.ltd>
  7. * @package zin
  8. * @version $Id
  9. * @link https://www.zentao.net
  10. */
  11. namespace zin;
  12. require_once __DIR__ . DS . 'node.class.php';
  13. require_once __DIR__ . DS . 'directive.class.php';
  14. require_once __DIR__ . DS . 'context.func.php';
  15. class htm extends node
  16. {
  17. /**
  18. * @param mixed $child
  19. * @param string $name
  20. * @param bool $prepend
  21. */
  22. public function addToBlock($name, $child, $prepend = false)
  23. {
  24. if(is_string($child))
  25. {
  26. $child = (object)array('html' => $child);
  27. }
  28. return parent::addToBlock($name, $child, $prepend);
  29. }
  30. }
  31. /**
  32. * @param mixed ...$codes
  33. */
  34. function html(...$codes)
  35. {
  36. return new htm(...$codes);
  37. }
  38. /**
  39. * 生成 rawContent 标记,可以指定名称,如果不指定则指向全局内容。
  40. * Generate rawContent tag, you can specify the name, if not specified, it will point to the global content.
  41. *
  42. * @param string|null $name 标记名称
  43. * @return htm 包含 rawContent 标记的 htm 对象
  44. */
  45. function rawContent($name = null)
  46. {
  47. if($name)
  48. {
  49. if(!isset(context()->rawContentNames[$name])) context()->rawContentNames[$name] = 0;
  50. return h::comment('{{RAW_CONTENT:' . $name . '}}');
  51. }
  52. context()->rawContentCalled = true;
  53. return h::comment('{{RAW_CONTENT}}');
  54. }
  55. /**
  56. * 生成 rawContent 开始标记。
  57. * Generate rawContent start tag.
  58. *
  59. * @param string $name 标记名称
  60. * @return void
  61. */
  62. function rawContentStart($name)
  63. {
  64. context()->rawContentNames[$name] = 0; // 0 means start.
  65. echo "<!-- {{RAW_CONTENT_START:$name}} -->";
  66. }
  67. /**
  68. * 生成 rawContent 结束标记。
  69. * Generate rawContent end tag.
  70. *
  71. * @param string $name 标记名称
  72. * @return void
  73. */
  74. function rawContentEnd($name)
  75. {
  76. $rawContentNames = context()->rawContentNames;
  77. if(isDebug())
  78. {
  79. $last = end($rawContentNames);
  80. $lastName = key($rawContentNames);
  81. if($lastName !== $name) triggerError("rawContentEnd(\"$name\") end called without rawContentStart(\"$lastName\").");
  82. elseif($last === 1) triggerError("rawContentEnd(\"$name\") already called.");
  83. }
  84. $rawContentNames[$name] = 1; // 1 means end.
  85. echo "<!-- {{RAW_CONTENT_END:$name}} -->";
  86. }
  87. /**
  88. * 解析 rawContent 标记,提取内容块。
  89. * Parse rawContent tag, extract content block.
  90. *
  91. * @param string $rawContent 原始内容字符串
  92. * @return array 包含所有内容块的数组,键为块名,值为块内容
  93. */
  94. function parseRawContent($rawContent)
  95. {
  96. $map = array('GLOBAL' => '');
  97. $offset = 0;
  98. $lastNoEndName = '';
  99. while($offset <= strlen($rawContent))
  100. {
  101. $startResult = preg_match('/<!-- \{\{RAW_CONTENT_START:([a-zA-Z0-9_]+)\}\} -->\n?/', $rawContent, $matches, PREG_OFFSET_CAPTURE, $offset);
  102. if($startResult !== 1)
  103. {
  104. if(!$lastNoEndName) $map['GLOBAL'] .= substr($rawContent, $offset);
  105. break;
  106. }
  107. if($lastNoEndName) $map[$lastNoEndName] = substr($rawContent, $offset, $matches[0][1] - $offset - (($offset > 0 && $rawContent[$offset - 1] === "\n") ? 1 : 0));
  108. elseif($offset === 0 && $matches[0][1] > 0) $map['GLOBAL'] = substr($rawContent, 0, $matches[0][1]);
  109. $name = $matches[1][0];
  110. $offset = $matches[0][1] + strlen($matches[0][0]);
  111. $endResult = preg_match("/\n?<!-- \{\{RAW_CONTENT_END:$name\}\} -->\n?/", $rawContent, $matches, PREG_OFFSET_CAPTURE, $offset);
  112. if($endResult === 1)
  113. {
  114. $map[$name] = substr($rawContent, $offset, $matches[0][1] - $offset);
  115. $offset = $matches[0][1] + strlen($matches[0][0]);
  116. $lastNoEndName = '';
  117. }
  118. else
  119. {
  120. $lastNoEndName = $name;
  121. }
  122. }
  123. if($lastNoEndName) $map[$lastNoEndName] = substr($rawContent, $offset);
  124. return $map;
  125. }
  126. function hookContent()
  127. {
  128. context()->hookContentCalled = true;
  129. return h::comment('{{HOOK_CONTENT}}');
  130. }