Skip to content

Commit

Permalink
links in shape; code run
Browse files Browse the repository at this point in the history
  • Loading branch information
schplurtz committed Oct 23, 2017
1 parent 0625c46 commit a894715
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
35 changes: 27 additions & 8 deletions ASCIIToSVG.php
Expand Up @@ -2517,7 +2517,11 @@ public function render() {

$objW = $maxX - $minX;
$objH = $maxY - $minY;


if (isset($this->options['a2s:link'])) {
$out .="<a xlink:href=\"".$this->options['a2s:link']."\">";
}

$i = 0;
foreach ($object as $o) {
$id = self::$id++;
Expand Down Expand Up @@ -2552,6 +2556,11 @@ public function render() {
$out .= "\t" . $text->render() . "\n";
}
}

if (isset($this->options['a2s:link'])) {
$out .="</a>";
}

$out .= "</g>\n";

/* Bazinga. */
Expand Down Expand Up @@ -2677,22 +2686,32 @@ public function render() {
$this->options['fill'] = '#fff';
}


$out .= "\t<path id=\"path{$this->name}\" ";
$out_p = "\t<path id=\"path{$this->name}\" ";
foreach ($this->options as $opt => $val) {
if (strpos($opt, 'a2s:', 0) === 0) {
continue;
if ($opt=='a2s:link') {
$alnk = $val;
}
continue;
}
$out .= "$opt=\"$val\" ";
$out_p .= "$opt=\"$val\" ";
}
if (isset($alnk)) {
$out_p = "\t<a xlink:href=\"".$alnk."\">".$out_p;
}
$out .= "d=\"{$path}\" />\n";
$out_p .= "d=\"{$path}\" />\n";

if (count($this->text) > 0) {
foreach ($this->text as $text) {
$out .= "\t" . $text->render() . "\n";
$out_p .= "\t" . $text->render() . "\n";
}
}

if (isset($alnk)) {
$out_p .= '</a>';
}
$out .= $out_p;

$bound = count($this->ticks);
for ($i = 0; $i < $bound; $i++) {
$t = $this->ticks[$i];
Expand Down Expand Up @@ -2735,7 +2754,7 @@ private static function svgEntities($str) {
*
* We want to replace these in text without confusing SVG.
*/
$s = array('&', '<', '>');
$s = array('&','<', '>');
$r = array('&amp;', '&lt;', '&gt;');
return str_replace($s, $r, $str);
}
Expand Down
15 changes: 11 additions & 4 deletions demo.txt
Expand Up @@ -70,7 +70,7 @@ Install the plugin using the [[doku>plugin:plugin|Plugin Manager]]. It will inst
====>| (svg) World '------. / #------# .-------. |
| .-----# |==+ | | | | |
'----# '---------# #------# '-------'
[0]: {"fill":"#CE3B83","stroke":"#F50078","stroke-dasharray":"5 5","stroke-width":4,"a2s:type":"cloud","a2s:delref":true}
[0]: {"a2s:link":"https://github.com/schplurtz/a2s","fill":"#CE3B83","stroke":"#F50078","stroke-dasharray":"5 5","stroke-width":4,"a2s:type":"cloud","a2s:delref":true}
</a2s>
<code txt tryme.a2s>
<a2s>
Expand All @@ -83,7 +83,7 @@ Install the plugin using the [[doku>plugin:plugin|Plugin Manager]]. It will inst
====>| (svg) World '------. / #------# .-------. |
| .-----# |==+ | | | | |
'----# '---------# #------# '-------'
[0]: {"fill":"#CE3B83","stroke":"#F50078","stroke-dasharray":"5 5","stroke-width":4,"a2s:type":"cloud","a2s:delref":true}
[0]: {"a2s:link":"https://github.com/schplurtz/a2s","fill":"#CE3B83","stroke":"#F50078","stroke-dasharray":"5 5","stroke-width":4,"a2s:type":"cloud","a2s:delref":true}
</a2s>
</code>

Expand All @@ -104,7 +104,7 @@ Please, note that the reference doc is on [[https://github.com/dhobsd/asciitosvg
- Identify the shape : In the **top left** angle of the shape, put some text between square bracket. example ''[0]'', ''[logo]'', ''[some text]''
- **At the bottom** of the drawing, for each identified shape, add a **single** line with the identifier and a JSON hash of property:value. See the section just below for details.

==== Styling shapes ====
==== Styling shapes and linking ====

Colors are expressed either
* In RGB with 3 or 6 hexdigits preceded by a sharp sign : ''#88d'', ''#F50078''
Expand All @@ -130,11 +130,18 @@ Available styling parametre :
</code>
* shape : ''"a2s:type":"shapename"'' where shapename is one of ''cloud'', ''computer'', ''diamond'', ''document'', ''storage'', ''printer''. Examples<code>"a2s:type":"cloud"
"a2s:type":"storage"
</code>
* link : ''"a2s:link":"//linktext//"'' where //linktext// is either an internal wiki link, or a classic //%%protocol://server/resource%%// link. [[doku>interwiki|interwiki links]] cannot yet be used. Since SVG can be included in ODT file, a2s does not obey the [[doku>config:canonical|canonical]] setting and always generate absolute links.\\ Examples:<code>
"a2s:link":"[[.:]]"
"a2s:link":"[[:start]]"
"a2s:link":"[[:start|The start page]]"
"a2s:link":"https://github.com/schplurtz/a2s"
"a2s:link":"ftp://ftp.debian.org/debian/README"
</code>
* identifier removal. By default the id is left in the image. This is useful when you have enough room to use a meaningful word as id. However, it is possible to remove it using this code <code>"a2s:delref":true</code>

here is a full example :
<code>[0]: {"fill":"#d48","stroke":"#F50078","stroke-dasharray":"5 5","stroke-width":4,"a2s:type":"cloud","a2s:delref":true}</code>
<code>[0]: {"fill":"#d48","stroke":"#F50078","stroke-dasharray":"5 5","stroke-width":4,"a2s:type":"cloud","a2s:delref":true,"a2s:link":"https://github.com/schplurtz/a2s"}</code>

==== Alignment ====

Expand Down
24 changes: 23 additions & 1 deletion syntax.php
Expand Up @@ -91,7 +91,7 @@ public function handle($match, $state, $pos, Doku_Handler $handler){
self::$align=$align; // needed to pass $align to ODT LEXER_MATCHED render
return array($state, $align, null); // odt renderer expects array size 3
case DOKU_LEXER_UNMATCHED :
$o = new dokuwiki\plugin\a2s\ASCIIToSVG(trim($match, "\r\n"));
$o = new dokuwiki\plugin\a2s\ASCIIToSVG($this->_prepare($match));
$o->setDimensionScale(9, 16);
$o->parseGrid();
// save alignment for later use by ODT renderer
Expand Down Expand Up @@ -177,6 +177,28 @@ protected function _extract_XY_4svg( $svgtxt ) {
// assume a 96 dpi screen
return array_map( function($v) { return ($v/96.0)."in"; }, $sizes );
}
/**
* Prepare matched text for beeing parsed. Removes unnecessary blank lines
* expand wikilinks to http absolute links. (absolute links because of
* ODT export)
*
* @param String $text The matched a2s input string
* @return String the prepared string
*/
protected function _prepare( $text ) {
return preg_replace_callback(
'/"a2s:link":"
\\[\\[
([^]|]*) # The page_id
(\\|[^]]*)? # |description optional
]]"
/x',
function( $match ) {
return '"a2s:link":"' . wl( cleanID($match[1]), '', true ) . '"';
},
trim($text, "\r\n")
);
}
}

// vim:ts=4:sw=4:et:

0 comments on commit a894715

Please sign in to comment.