-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement r2snip to ease the maintainance and creation of code snippets
- Loading branch information
Showing
5 changed files
with
98 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
R2PNG=node r2png/index.js | ||
R2SNIP=node r2snip.js | ||
D=../img-white/ | ||
|
||
all: | ||
$(R2SNIP) `find ../| grep snippets$$` | ||
mkdir -p $(D)/configuration | ||
$(R2PNG) bins/ls 'e??~color' > $(D)/configuration/e--color.png | ||
#$(R2PNG) bins/ls 'e??~color' > $(D)/configuration/e--color.png | ||
$(R2PNG) - 'hello world' > a.png | ||
#$(R2PNG) bins/ls 'pd 1 @ 0x00404894' > output.png | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const snippets = process.argv.slice(2); | ||
const { spawnSync } = require('child_process'); | ||
|
||
for (let snip of snippets) { | ||
const dir = path.dirname(snip); | ||
const text = fs.readFileSync(snip); | ||
parseSnippet(dir, text.toString('utf8')) | ||
} | ||
|
||
function system(cmd, args) { | ||
const result = spawnSync(cmd, args, { stdio: ['ignore', 'pipe', 2]}); | ||
return result.stdout; | ||
} | ||
|
||
function buildSnippetImage(output, mode, args) { | ||
if (mode === 'string') { | ||
console.error('PNG STR', output); | ||
const png = system("node", ['r2png/index.js', '"', ...args]); | ||
fs.writeFileSync(output, png); | ||
} else { | ||
// we need to parse the first arg to get the filename. lets use - for now | ||
console.error('PNG R2R', output); | ||
const png = system("node", ['r2png/index.js', ...args]); | ||
fs.writeFileSync(output, png); | ||
} | ||
} | ||
|
||
function parseSnippet(dir, text) { | ||
var filename = ''; | ||
var readMode = ''; | ||
var args = []; | ||
var comment = {}; | ||
for (let _line of text.split('\n')) { | ||
const line = _line.trim(); | ||
if (readMode) { | ||
if (line === readMode) { | ||
const mode = (readMode === '"')? 'string': 'commands'; | ||
if (mode === 'commands') { | ||
args = [comment.open, ...args]; | ||
} | ||
// console.error("RUN", filename, mode, args); | ||
buildSnippetImage(path.join(dir, filename), mode, args); | ||
readMode = ''; | ||
args = []; | ||
continue; | ||
} | ||
args.push(line); | ||
continue; | ||
} | ||
if (line.startsWith('//')) { | ||
const words = line.substring(2).trim().split(' '); | ||
comment[words[0]] = words[1]; | ||
} | ||
if (line.endsWith('"')) { | ||
if (line.endsWith("(\"")) { | ||
filename = line.substring(0, line.length - 2).trim(); | ||
readMode = '\")'; | ||
} else { | ||
filename = line.substring(0, line.length - 1).trim(); | ||
readMode = '\"'; | ||
} | ||
} | ||
} | ||
} |