Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active January 29, 2019 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfinke/5ad47ff24cd2ba049dcf982f933053b4 to your computer and use it in GitHub Desktop.
Save dfinke/5ad47ff24cd2ba049dcf982f933053b4 to your computer and use it in GitHub Desktop.
$code = @"
Paper 100
Pen 0
Line 50 77 22 27
Line 22 27 78 27
Line 78 27 50 77
"@
$svg = @()
try {
$pgm = $code.Split("`n")
$tokens = $pgm -split "[\t\f\v ]+"
$tokenCount = $tokens.Count
$svg = @()
for ($idx = 0; $idx -lt $tokenCount - 1; $idx++) {
$token = $tokens[$idx]
switch ($token) {
"Paper" {
[int]$paperColor = $tokens[++$idx]
$svg += '<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" version="1.1">'
}
"Pen" {
[int]$color = $tokens[++$idx]
$rgb = "rgb({0}%, {0}%, {0}%)" -f $color
$svg += "`t" + "<rect x='0' y='0' width='100' height='100' fill='$rgb'></rect>"
}
"Line" {
$x1 = $tokens[++$idx]
$y1 = $tokens[++$idx]
$x2 = $tokens[++$idx]
$y2 = $tokens[++$idx]
$rgb = "rgb({0}%, {0}%, {0}%)" -f $paperColor
$svg += "`t" + "<line x1='$x1' y1='$y1' x2='$x2' y2='$y2' stroke='$rgb' stroke-linecap='round'></line>"
}
}
}
$svg += '</svg>'
$html = $svg -join "`r`n"
} catch {
$html = $Error[0].exception.innerException.ToString()
}
$html
@dfinke
Copy link
Author

dfinke commented Jan 29, 2019

<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" version="1.1">
	<rect x='0' y='0' width='100' height='100' fill='rgb(0%, 0%, 0%)'></rect>
	<line x1='50' y1='77' x2='22' y2='27' stroke='rgb(100%, 100%, 100%)' stroke-linecap='round'></line>
	<line x1='22' y1='27' x2='78' y2='27' stroke='rgb(100%, 100%, 100%)' stroke-linecap='round'></line>
	<line x1='78' y1='27' x2='50' y2='77' stroke='rgb(100%, 100%, 100%)' stroke-linecap='round'></line>
</svg>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment