Skip to content

Commit

Permalink
Refactored section edit
Browse files Browse the repository at this point in the history
The data for the section edit button is now passed as an assoziative array which is
encoded in the '#<!-- EDIT(.*) -->#' placeholder as an JSON array.
  • Loading branch information
lpaulsen93 committed Feb 15, 2018
1 parent 96dc7be commit ec57f11
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 37 deletions.
15 changes: 8 additions & 7 deletions _test/tests/inc/html_secedit_pattern.test.php
Expand Up @@ -6,9 +6,9 @@ class html_scedit_pattern_test extends DokuWikiTest {
public function dataProviderForTestSecEditPattern() {
return [
[
'<!-- EDIT5 SECTION "Plugins" "plugins" [1406-] -->',
'<!-- EDIT{"target":"SECTION","name":"Plugins","hid":"plugins","codeblockOffset":0,"secid":5,"range":"1406-"} -->',
[
'secid' => '5',
'secid' => 5,
'target' => 'SECTION',
'name' => 'Plugins',
'hid' => 'plugins',
Expand All @@ -17,9 +17,9 @@ public function dataProviderForTestSecEditPattern() {
'basic section edit',
],
[
'<!-- EDIT10 TABLE "" "table4" [11908-14014] -->',
'<!-- EDIT{"target":"TABLE","name":"","hid":"table4","codeblockOffset":0,"secid":10,"range":"11908-14014"} -->',
[
'secid' => '10',
'secid' => 10,
'target' => 'TABLE',
'name' => '',
'hid' => 'table4',
Expand All @@ -28,9 +28,9 @@ public function dataProviderForTestSecEditPattern() {
'table edit'
],
[
'<!-- EDIT2 PLUGIN_DATA [27-432] -->',
'<!-- EDIT{"target":"PLUGIN_DATA","name":"","hid":"","codeblockOffset":0,"secid":2,"range":"27-432"} -->',
[
'secid' => '2',
'secid' => 2,
'target' => 'PLUGIN_DATA',
'name' => '',
'hid' => '',
Expand All @@ -50,8 +50,9 @@ public function dataProviderForTestSecEditPattern() {
*/
public function testSecEditPattern($text, $expectedMatches, $msg) {
preg_match(SEC_EDIT_PATTERN, $text, $matches);
$data = json_decode($matches[1], true);
foreach ($expectedMatches as $key => $expected_value) {
$this->assertSame($expected_value, $matches[$key], $msg);
$this->assertSame($expected_value, $data[$key], $msg);
}
}

Expand Down
22 changes: 10 additions & 12 deletions inc/html.php
Expand Up @@ -9,7 +9,7 @@
if(!defined('DOKU_INC')) die('meh.');
if(!defined('NL')) define('NL',"\n");
if (!defined('SEC_EDIT_PATTERN')) {
define('SEC_EDIT_PATTERN', '#<!-- EDIT(?<secid>\d+) (?<target>[A-Z_]+) (?:"(?<name>[^"]*)" )?(?:"(?<hid>[^"]*)" )?\[(?<range>\d+-\d*)\] -->#');
define('SEC_EDIT_PATTERN', '#<!-- EDIT({.*}) -->#');
}


Expand Down Expand Up @@ -114,17 +114,12 @@ function html_secedit($text,$show=true){
* @triggers HTML_SECEDIT_BUTTON
*/
function html_secedit_button($matches){
$data = array('secid' => $matches['secid'],
'target' => strtolower($matches['target']),
'range' => $matches['range']);

if (!empty($matches['hid'])) {
$data['hid'] = strtolower($matches['hid']);
}

if (!empty($matches['name'])) {
$data['name'] = $matches['name'];
$data = json_decode($matches[1], true);
if ($data == NULL) {
return;
}
$data ['target'] = strtolower($data['target']);
$data ['hid'] = strtolower($data['hid']);

return trigger_event('HTML_SECEDIT_BUTTON', $data,
'html_secedit_get_button');
Expand Down Expand Up @@ -1860,6 +1855,9 @@ function html_edit(){
if ($INPUT->has('hid')) {
$form->addHidden('hid', $INPUT->str('hid'));
}
if ($INPUT->has('codeblockOffset')) {
$form->addHidden('codeblockOffset', $INPUT->str('codeblockOffset'));
}
$form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar', 'class'=>'editBar')));
$form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
$form->addElement(form_makeCloseTag('div'));
Expand Down Expand Up @@ -2314,4 +2312,4 @@ function html_sizechange($sizechange, Doku_Form $form) {
$form->addElement($value);
$form->addElement(form_makeCloseTag('span'));
}
}
}
63 changes: 45 additions & 18 deletions inc/parser/xhtml.php
Expand Up @@ -59,16 +59,31 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
/**
* Register a new edit section range
*
* @param string $type The section type identifier
* @param string $title The section title
* @param int $start The byte position for the edit start
* @param array $data Associative array with section data:
* Key 'name': the section name/title
* Key 'target': the target for the section edit,
* e.g. 'section' or 'table'
* Key 'hid': header id
* Key 'codeblockOffset': actual code block index
* Key 'start': set in startSectionEdit(),
* do not set yourself
* Key 'range': calculated from 'start' and
* $key in finishSectionEdit(),
* do not set yourself
* @return string A marker class for the starting HTML element
*
* @author Adrian Lang <lang@cosmocode.de>
*/
public function startSectionEdit($start, $type, $title = null, $hid = null) {
$this->sectionedits[] = array(++$this->lastsecid, $start, $type, $title, $hid);
return 'sectionedit'.$this->lastsecid;
public function startSectionEdit($start, $data) {
if (!is_array($data)) {
msg('startSectionEdit: $data is NOT an array!', -1);
return '';
}
$data['secid'] = ++$this->lastsecid;
$data['start'] = $start;
$this->sectionedits[] = $data;
return 'sectionedit'.$data['secid'];
}

/**
Expand All @@ -79,18 +94,16 @@ public function startSectionEdit($start, $type, $title = null, $hid = null) {
* @author Adrian Lang <lang@cosmocode.de>
*/
public function finishSectionEdit($end = null, $hid = null) {
list($id, $start, $type, $title, $hid) = array_pop($this->sectionedits);
if(!is_null($end) && $end <= $start) {
$data = array_pop($this->sectionedits);
if(!is_null($end) && $end <= $data['start']) {
return;
}
$this->doc .= "<!-- EDIT$id ".strtoupper($type).' ';
if(!is_null($title)) {
$this->doc .= '"'.str_replace('"', '', $title).'" ';
}
if(!is_null($hid)) {
$this->doc .= '"'.$hid.'" ';
$data['hid'] .= $hid;
}
$this->doc .= "[$start-".(is_null($end) ? '' : $end).'] -->';
$data['range'] = $data['start'].'-'.(is_null($end) ? '' : $end);
unset($data['start']);
$this->doc .= '<!-- EDIT'.json_encode ($data).' -->';
}

/**
Expand All @@ -117,7 +130,7 @@ function document_start() {
function document_end() {
// Finish open section edits.
while(count($this->sectionedits) > 0) {
if($this->sectionedits[count($this->sectionedits) - 1][1] <= 1) {
if($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) {
// If there is only one section, do not write a section edit
// marker.
array_pop($this->sectionedits);
Expand Down Expand Up @@ -212,15 +225,20 @@ function header($text, $level, $pos) {

if($level <= $conf['maxseclevel'] &&
count($this->sectionedits) > 0 &&
$this->sectionedits[count($this->sectionedits) - 1][2] === 'section'
$this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section'
) {
$this->finishSectionEdit($pos - 1);
}

// write the header
$this->doc .= DOKU_LF.'<h'.$level;
if($level <= $conf['maxseclevel']) {
$this->doc .= ' class="'.$this->startSectionEdit($pos, 'section', $text, $hid).'"';
$data = array();
$data['target'] = 'section';
$data['name'] = $text;
$data['hid'] = $hid;
$data['codeblockOffset'] = $this->_codeblock;
$this->doc .= ' class="'.$this->startSectionEdit($pos, $data).'"';
}
$this->doc .= ' id="'.$hid.'">';
$this->doc .= $this->_xmlEntities($text);
Expand Down Expand Up @@ -632,6 +650,7 @@ function code($text, $language = null, $filename = null) {
function _highlight($type, $text, $language = null, $filename = null) {
global $ID;
global $lang;
global $INPUT;

$language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language);

Expand All @@ -641,8 +660,12 @@ function _highlight($type, $text, $language = null, $filename = null) {
$class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
$class = 'mediafile mf_'.$class;

$offset = 0;
if ($INPUT->has('codeblockOffset')) {
$offset = $INPUT->str('codeblockOffset');
}
$this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
$this->doc .= '<dt><a href="'.exportlink($ID, 'code', array('codeblock' => $this->_codeblock)).'" title="'.$lang['download'].'" class="'.$class.'">';
$this->doc .= '<dt><a href="'.exportlink($ID, 'code', array('codeblock' => $offset+$this->_codeblock)).'" title="'.$lang['download'].'" class="'.$class.'">';
$this->doc .= hsc($filename);
$this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
}
Expand Down Expand Up @@ -1340,7 +1363,11 @@ function table_open($maxcols = null, $numrows = null, $pos = null, $classes = nu
}
if($pos !== null) {
$hid = $this->_headerToLink($class, true);
$class .= ' '.$this->startSectionEdit($pos, 'table', '', $hid);
$data = array();
$data['target'] = 'table';
$data['name'] = '';
$data['hid'] = $hid;
$class .= ' '.$this->startSectionEdit($pos, $data);
}
$this->doc .= '<div class="'.$class.'"><table class="inline">'.
DOKU_LF;
Expand Down

0 comments on commit ec57f11

Please sign in to comment.