A small Javascript library to convert an ASCII representation of a Go (Baduk|Weiqi) diagram into an SVG drawing.
GoDiagramsJS is a straight port to Javascript of sltxt2png.php, the PHP tool Arno Hollosi and Morten Pahle created for their Go site Sensei Library. The only major difference is in the output: instead of a bitmap image, GoDiagramsJS produces an SVG drawing embedded in an element ready for inclusion in HTML code.
Example: ASCII representation
$$c Nadare joseki
$$ --------------
$$ | . . . . . . .
$$ | . . . . . . .
$$ | . . . 1 6 . .
$$ | . . 5 4 . . .
$$ | . . 3 2 . . .
$$ | . . . . . . .
$$ | . . . . . . .
And a bitmap rendered version:
The diagram's element may include markup (circles, squares, letters, etc.) and each element may include a link. Arrows and areas can also be visualized. See the Sensei library page for a complete description of the ASCII board syntax. A brief summary is included in the header of the javascript file.
All the basic components (stones, lines, goban, etc.) are identified by classes and can be styled with an external CSS sheet. A skeletal CSS file is provide as proof of concept.
Example of use:
// Create a div element in the html document
var goDiagDiv = document.createElement("div");
// Import the GoDiagram class into your project and instantiate it
var GoDiagram = require("./slt2svg.js").GoDiagram;
// Pass an ASCII diagram to the class constructor.
var sampleAscii = '$$c Nadare joseki
$$ --------------
$$ | . . . . . . .
$$ | . . . . . . .
$$ | . . . 1 6 . .
$$ | . . 5 4 . . .
$$ | . . 3 2 . . .
$$ | . . . . . . .
$$ | . . . . . . .';
renderer = new GoDiagram(sampleAscii);
// Ask the instance to parse the ASCII diagram and create an SVG representation of it.
// A string containing the whole rendered diagram as an SVG element will be returned.
// If parsing fails, the SVG will be a red box with an error message in it.
sampleSvg = renderer.createSVG();
// Add the SVG as pure HTML
goDiagDiv.innerHTML = sampleSvg;
// Insert the div into your DOM
Original sl2png.php library: Copyright (C) 2001-2004 Arno Hollosi (ahollosi@xmp.net) and Morten Pahle (morten@pahle.org.uk).
Javascript code: Copyright (C) 2019 Stefano Franchi 2019 (stefano.franchi@gmail.com).
Released under the GPL v. 3.0 or later.