Created
May 19, 2017 16:47
-
-
Save papodaca/2a93dd153e1912a9cfd8f226fcd57201 to your computer and use it in GitHub Desktop.
Convert ansi files output by THE DRAW to html
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
/* | |
Copyright © Ethan Apodaca 2016 | |
[email protected] | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
usage: | |
g++ -std=c++11 ascii2html.cpp -o ascii2html | |
iconv -f 437 SOMEFILE.ANS | ./ascii2html > tower.html | |
Add output to webpage | |
*/ | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
// For esc-codes details: http://ascii-table.com/ansi-escape-sequences.php | |
char enders[14] = {'H', 'f', 'A', 'B', 'C', 'D', 's', 'u', 'J', 'K', 'm', 'h', 'l', 'p'}; | |
enum color { | |
BLACK = 30, | |
RED = 31, | |
GREEN = 32, | |
YELLOW = 33, | |
BLUE = 34, | |
MAGENTA = 35, | |
CYAN = 36, | |
WHITE = 37 | |
}; | |
#define idName(x) case x: return #x | |
string ColorToName(int id) { | |
switch(id) { | |
idName(BLACK); | |
idName(RED); | |
idName(GREEN); | |
idName(YELLOW); | |
idName(BLUE); | |
idName(MAGENTA); | |
idName(CYAN); | |
idName(WHITE); | |
default: return NULL; | |
} | |
} | |
bool isEnder(char character) { | |
for(int ii = 0; ii< 14; ii++) { | |
if(enders[ii] == character) { | |
return true; | |
} | |
} | |
return false; | |
} | |
bool firstSpan = true; | |
bool bold = false; | |
int colorCode = -1; | |
int bgColorCode = -1; | |
string generateSpan() { | |
string classes = ""; | |
if(colorCode != -1) { | |
classes = (bold?"dark-":"") + ColorToName(colorCode); | |
} | |
if(bgColorCode != -1) { | |
classes = classes + " bg-" + (bold?"dark-":"") + ColorToName(bgColorCode); | |
} | |
transform(classes.begin(), classes.end(), classes.begin(), ::tolower); | |
return "<span class=\"" + classes + "\">"; | |
} | |
void processCode(string code) { | |
istringstream ss(code); | |
string part; | |
while(std::getline(ss, part, ';')) { | |
int numCode = std::stoi(part.c_str()); | |
if(numCode == 0) { | |
bold = false; | |
bgColorCode = -1; | |
colorCode = -1; | |
} else if(numCode == 1) { | |
bold = true; | |
} else if(numCode>=30 && numCode <=37) { | |
colorCode = numCode; | |
} else if(numCode>=40 && numCode <=47) { | |
bgColorCode = numCode - 10; | |
} | |
} | |
if(!firstSpan) { | |
cout<< "</span>"; | |
} else { | |
firstSpan = false; | |
} | |
cout << generateSpan(); | |
} | |
int main() { | |
for (string line; getline(cin, line);) { | |
for (auto ii = 0 ; ii < line.length(); ii++) { | |
char character = line[ii]; | |
if(character == '\x1b') { | |
string code = ""; | |
while(!isEnder(character)) { | |
character = line[++ii]; | |
if(character != '[' && !isEnder(character)) { | |
code = code + character; | |
} | |
if(isEnder(character)) { | |
if(character == 'm') { | |
processCode(code); | |
} else if(character == 'C') { | |
for(auto ii=std::stoi(code.c_str());ii>0;ii--) { | |
cout << "</span> "; | |
} | |
cout << generateSpan(); | |
} | |
} | |
} | |
} else { | |
if(character == ' ') { | |
cout << "</span> " << generateSpan(); | |
} else { | |
cout << character; | |
} | |
} | |
} | |
} | |
cout << "</span>"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment