1<?php 2/** 3 * DokuWiki Plugin schemadata (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Deniz Kilic <fz.deniz.kilic@gmail.com> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) { 11 die(); 12} 13 14class syntax_plugin_schemadata extends DokuWiki_Syntax_Plugin 15{ 16 protected $doi = null; 17 /** 18 * @return string Syntax mode type 19 */ 20 public function getType() 21 { 22 return 'substition'; 23 } 24 25 /** 26 * @return string Paragraph type 27 */ 28 public function getPType() 29 { 30 return 'normal'; 31 } 32 33 /** 34 * @return int Sort order - Low numbers go before high numbers 35 */ 36 public function getSort() 37 { 38 return 999; 39 } 40 41 /** 42 * Connect lookup pattern to lexer. 43 * 44 * @param string $mode Parser mode 45 */ 46 public function connectTo($mode) 47 { 48 // $this->Lexer->addSpecialPattern('<FIXME>', $mode, 'plugin_schemadata'); 49 $this->Lexer->addEntryPattern('<doi>', $mode, 'plugin_schemadata'); 50 } 51 52 public function postConnect() 53 { 54 $this->Lexer->addExitPattern('</doi>', 'plugin_schemadata'); 55 } 56 57 /** 58 * Handle matches of the schemadata syntax 59 * 60 * @param string $match The match of the syntax 61 * @param int $state The state of the handler 62 * @param int $pos The position in the document 63 * @param Doku_Handler $handler The handler 64 * 65 * @return array Data for the renderer 66 */ 67 public function handle($match, $state, $pos, Doku_Handler $handler) 68 { 69 switch ($state) { 70 case DOKU_LEXER_ENTER : 71 return false; 72 case DOKU_LEXER_UNMATCHED : 73 $this->doi = $match; 74 return false; 75 case DOKU_LEXER_EXIT : 76 $data = array($state, $this->doi); 77 $this->doi = null; 78 return $data; 79 } 80 return array(); 81 } 82 83 /** 84 * Render xhtml output or metadata 85 * 86 * @param string $mode Renderer mode (supported modes: xhtml) 87 * @param Doku_Renderer $renderer The renderer 88 * @param array $data The data from the handler() function 89 * 90 * @return bool If rendering was successful. 91 */ 92 public function render($mode, Doku_Renderer $renderer, $data) 93 { 94 list($state, $doi) = $data; 95 if ($mode != 'xhtml') return false; 96 97 // modified version of https://gist.github.com/kjgarza/34ca6a866b4437bc1c07d84f208a01c4 98 $html = '<script type="text/javascript">'."\n".'/*<![CDATA[*/'; 99 $html .= '(function($){ 100 $(document).ready( 101 function() { 102 var doi = '; 103 $html .= "'$doi';"; //for example 10.5284/1015681 104 $html .= 'if (doi === undefined) {return;} 105 var url = "https://data.crosscite.org/application/vnd.schemaorg.ld+json/" 106 url += doi 107 108 $.ajax({ 109 url: url, 110 dataType: \'text\', // don\'t convert JSON to Javascript object 111 success: function(data) { 112 $(\'<script>\') 113 .attr(\'type\', \'application/ld+json\') 114 .text(data) 115 .appendTo(\'head\'); 116 }, 117 error: function (error) { 118 console.log(error.responseJSON); 119 } 120 }); 121 }); 122 })(jQuery)'; 123 $html.= '/*!]]>*/'."\n".'</script>'."\n"; 124 $renderer->doc .= $html; 125 return true; 126 } 127} 128 129