header.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Include plugin (permalink header component)
  4. *
  5. * Provides a header instruction which renders a permalink to the included page
  6. *
  7. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  8. * @author Gina Haeussge <osd@foosel.net>
  9. * @author Michael Klier <chi@chimeric.de>
  10. */
  11. class syntax_plugin_include_header extends DokuWiki_Syntax_Plugin {
  12. function getType() {
  13. return 'formatting';
  14. }
  15. function getSort() {
  16. return 50;
  17. }
  18. function handle($match, $state, $pos, Doku_Handler $handler) {
  19. // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser
  20. }
  21. /**
  22. * Renders a permalink header.
  23. *
  24. * Code heavily copied from the header renderer from inc/parser/xhtml.php, just
  25. * added an href parameter to the anchor tag linking to the wikilink.
  26. */
  27. function render($mode, Doku_Renderer $renderer, $data) {
  28. global $conf;
  29. list($headline, $lvl, $pos, $page, $sect, $flags) = $data;
  30. if ($mode == 'xhtml') {
  31. /** @var Doku_Renderer_xhtml $renderer */
  32. $hid = $renderer->_headerToLink($headline, true);
  33. $renderer->toc_additem($hid, $headline, $lvl);
  34. $url = ($sect) ? wl($page) . '#' . $sect : wl($page);
  35. $renderer->doc .= DOKU_LF.'<h' . $lvl;
  36. $classes = array();
  37. if($flags['taglogos']) {
  38. $tag = $this->_get_firsttag($page);
  39. if($tag) {
  40. $classes[] = 'include_firsttag__' . $tag;
  41. }
  42. }
  43. // the include header instruction is always at the beginning of the first section edit inside the include
  44. // wrap so there is no need to close a previous section edit.
  45. if ($lvl <= $conf['maxseclevel']) {
  46. if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions
  47. $classes[] = $renderer->startSectionEdit($pos, array('target' => 'section', 'name' => $headline, 'hid' => $hid));
  48. } else {
  49. $classes[] = $renderer->startSectionEdit($pos, 'section', $headline);
  50. }
  51. }
  52. if ($classes) {
  53. $renderer->doc .= ' class="'. implode(' ', $classes) . '"';
  54. }
  55. $headline = $renderer->_xmlEntities($headline);
  56. $renderer->doc .= ' id="'.$hid.'"><a href="' . $url . '" title="' . $headline . '">';
  57. $renderer->doc .= $headline;
  58. $renderer->doc .= '</a></h' . $lvl . '>' . DOKU_LF;
  59. return true;
  60. } else {
  61. $renderer->header($headline, $lvl, $pos);
  62. }
  63. return false;
  64. }
  65. /**
  66. * Optionally add a CSS class for the first tag
  67. *
  68. * @author Michael Klier <chi@chimeric.de>
  69. */
  70. function _get_firsttag($page) {
  71. if(plugin_isdisabled('tag') || (!plugin_load('helper', 'tag'))) {
  72. return false;
  73. }
  74. $subject = p_get_metadata($page, 'subject');
  75. if (is_array($subject)) {
  76. $tag = $subject[0];
  77. } else {
  78. list($tag, $rest) = explode(' ', $subject, 2);
  79. }
  80. if($tag) {
  81. return $tag;
  82. } else {
  83. return false;
  84. }
  85. }
  86. }
  87. // vim:ts=4:sw=4:et: