Skip to content

Instantly share code, notes, and snippets.

@papodaca
Last active October 1, 2016 16:30
Show Gist options
  • Save papodaca/ed5b81e9394796f2cefd18ee78aaa56e to your computer and use it in GitHub Desktop.
Save papodaca/ed5b81e9394796f2cefd18ee78aaa56e to your computer and use it in GitHub Desktop.
ASCII WEB ART
body {
background-color: black;
}
pre {
font-size:18px;
line-height: 20px;
font-family: monospace;
}
span {
height:20px;
}
.black, .dark-black {
color: #333;
}
.dark-blue {
color: #0000AA;
}
.dark-green {
color: #55FF55;
}
.dark-cyan {
color: #00AAAA;
}
.dark-red {
color: #AA0000;
}
.dark-magenta {
color: #AA00AA;
}
.brown {
color: #AA5500;
}
.gray {
color: #AAA;
}
.dark-gray {
color: #555;
}
.blue {
color: #5555FF;
}
.green {
color: #00AA00;
}
.cyan {
color: #55FFFF;
}
.red {
color: #FF5555;
}
.magenta {
color: #FF55FF;
}
.yellow {
color: #FFFF55;
}
.white {
color: #FFF;
}
.bg-black, .bg-dark-black {
background-color: #333;
}
.bg-dark-blue {
background-color: #0000AA;
}
.bg-dark-green {
background-color: #00AA00;
}
.bg-dark-cyan {
background-color: #00AAAA;
}
.bg-dark-red {
background-color: #AA0000;
}
.bg-dark-magenta {
background-color: #AA00AA;
}
.bg-brown {
background-color: #AA5500;
}
.bg-gray {
background-color: #AAA;
}
.bg-dark-gray {
background-color: #555;
}
.bg-blue {
background-color: #5555FF;
}
.bg-green {
background-color: #55FF55;
}
.bg-cyan {
background-color: #55FFFF;
}
.bg-red {
background-color: #FF5555;
}
.bg-magenta {
background-color: #FF55FF;
}
.bg-yellow {
background-color: #FFFF55;
}
.bg-white {
background-color: #FFF;
}
/*
Copyright © Ethan Apodaca 2016
[email protected]
usage:
g++ -std=c++11 ascii2html.cpp -o ascii2html
iconv -f 437 TOWER.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') {
cout << "</span>";
for(auto ii=std::stoi(code.c_str());ii>0;ii--) {
cout << "&nbsp;";
}
cout << generateSpan();
}
}
}
} else {
if(character == ' ') {
cout << "</span>&nbsp;" << generateSpan();
} else {
cout << character;
}
}
}
}
cout << "</span>";
return 0;
}
[?7hワワワワワワワ ワワ ワ ワワ ワワワワワワワ
ワ ロローワワ イ゚゚ イロイ゚ワワ ゚゚
イ イイ イロ ーイ゚゚゚ア゚アイワー イ゚
゚ ゚゚ー゚゚ ゚゚ ゚ ーロ ゚゚゚゚゚゚
ワワワワワワワ ワワワワワワワ ワワ ワワ ロロ ワワワワワワワ ワワワワワワ
ワ ロローワワ イ゚ワ ロイロローロ イロイ ロロイ゚ワワ ゚゚ ロローワ イロ
イ イイ イロ ーイワ゚ イ イイ ゚ワア゚゚ワイイ アイワー イ゚イイ イ ー゚
゚ ゚゚ー゚゚ ゚゚゚゚゚゚ ゚゚゚゚゚゚ワ゚゚゚゚ ゚゚゚゚゚゚ ゚゚ー
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment