Skip to content

Instantly share code, notes, and snippets.

@Southclaws
Last active April 25, 2017 23:35
Show Gist options
  • Save Southclaws/250ba701de0a9a1ab9a3 to your computer and use it in GitHub Desktop.
Save Southclaws/250ba701de0a9a1ab9a3 to your computer and use it in GitHub Desktop.
Very basic converter just for personal use since I generally write documentation in Markdown and converting to BBCode for SA:MP forums is pretty annoying.
var fs = require('fs');
var util = require('util');
var
regexHeader1 = new RegExp('^# (.*)'),
replcHeader1 = ['[COLOR="FF4700"][SIZE="7"][B]', '[/B][/SIZE][/COLOR]'],
regexHeader2 = new RegExp('^## (.*)'),
replcHeader2 = ['[COLOR="RoyalBlue"][SIZE="6"][B]', '[/B][/SIZE][/COLOR]'],
regexHeader3 = new RegExp('^### (.*)'),
replcHeader3 = ['[COLOR="DeepSkyBlue"][SIZE="5"][B]', '[/B][/SIZE][/COLOR]'],
replcItalics = ['[I]', '[/I]'],
regexBoldAster = new RegExp('\\*\\*'),
regexBoldUnder = new RegExp('__'),
replcBold = ['[B]', '[/B]'],
replcCodeBlock = ['[PAWN]', '[/PAWN]'],
regexCodeInlineOpen = new RegExp('(```).'),
regexCodeInlineClose = new RegExp('.(```)'),
replcCodeInline = ['[FONT="courier new"]', '[/FONT]'];
if(process.argv.length !== 3)
{
console.error('Usage: markdown-to-bbcode <filename>');
process.exit(1);
}
else if(!fs.existsSync(process.argv[2]))
{
console.error('Error: Invalid filename given');
process.exit(1);
}
else
{
var
line = fs.readFileSync(process.argv[2], { encoding: 'utf-8' }),
output = line.split('\r\n');
var
openItalics = false,
openBold = false,
openCodeInline = false,
openCodeBlock = false;
for (var i = 0; i < output.length; i++)
{
var match = regexHeader1.exec(output[i]);
if(match != null)
{
output[i] = replcHeader1[0]+match[1]+replcHeader1[1];
continue;
}
var match = regexHeader2.exec(output[i]);
if(match != null)
{
output[i] = replcHeader2[0]+match[1]+replcHeader2[1];
continue;
}
var match = regexHeader3.exec(output[i]);
if(match != null)
{
output[i] = replcHeader3[0]+match[1]+replcHeader3[1];
continue;
}
if(output[i] == "```")
{
if(openCodeBlock == false)
{
output[i] = replcCodeBlock[0];
openCodeBlock = true;
}
else
{
output[i] = replcCodeBlock[1];
openCodeBlock = false;
}
}
for (var j = 0; j < output[i].length; j++)
{
if(openCodeInline == false)
{
var match = regexCodeInlineOpen.exec(output[i]);
if(match != null)
{
var pos = output[i].search(match[1]);
output[i] = output[i].slice(0, pos) + replcCodeInline[0] + output[i].slice(pos + match[1].length);
j += replcCodeInline[0].length;
openCodeInline = true;
continue;
}
}
else
{
var match = regexCodeInlineClose.exec(output[i]);
if(match != null)
{
var pos = output[i].search(match[1]);
output[i] = output[i].slice(0, pos) + replcCodeInline[1] + output[i].slice(pos + match[1].length);
j += replcCodeInline[1].length;
openCodeInline = true;
}
continue;
}
if(openBold == false)
{
var match = regexBoldAster.exec(output[i]);
if(match == null)
match = regexBoldUnder.exec(output[i]);
if(match != null)
{
var pos = output[i].search(regexBoldAster);
output[i] = output[i].slice(0, pos) + replcBold[0] + output[i].slice(pos + 2);
j += replcBold[0].length;
openBold = true;
continue;
}
}
else
{
var match = regexBoldAster.exec(output[i]);
if(match == null)
match = regexBoldUnder.exec(output[i]);
if(match != null)
{
var pos = output[i].search(regexBoldAster);
output[i] = output[i].slice(0, pos) + replcBold[1] + output[i].slice(pos + 2);
j += replcBold[1].length;
openBold = false;
continue;
}
}
if(output[i][j] == '*' && output[i][j + 1] != '*' && !openCodeBlock)
{
if(j > 0 && output[i][j - 1] == '*')
continue;
if(openItalics == false)
{
output[i] = output[i].slice(0, j) + replcItalics[0] + output[i].slice(j+1);
j += replcItalics[0].length-1;
openItalics = true;
}
else
{
output[i] = output[i].slice(0, j) + replcItalics[1] + output[i].slice(j+1);
j += replcItalics[1].length-1;
openItalics = false;
}
}
if(output[i][j] == '_' && output[i][j + 1] != '_' && !openCodeBlock)
{
if(j > 0 && output[i][j - 1] == '_')
continue;
if(openItalics == false)
{
output[i] = output[i].slice(0, j) + replcItalics[0] + output[i].slice(j+1);
j += replcItalics[0].length-1;
openItalics = true;
}
else
{
output[i] = output[i].slice(0, j) + replcItalics[1] + output[i].slice(j+1);
j += replcItalics[1].length-1;
openItalics = false;
}
}
/*
if(output[i][j] == '_')
{
if(openItalics == false)
{
output[i] = output[i].slice(0, j) + replcItalics[0] + output[i].slice(j);
openItalics = true;
}
else
{
output[i] = output[i].slice(0, j) + replcItalics[1] + output[i].slice(j);
openItalics = false;
}
}*/
};
console.log(output[i]);
};
fs.writeFile("output.txt", output.join("\r\n"), function(err)
{
if(err)
{
console.log(err);
}
else
{
console.log("The file was saved!");
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment