2 The contents of this file are subject to the Mozilla Public License
3 Version 1.1 (the "License"); you may not use this file except in
4 compliance with the License. You may obtain a copy of the License at
5 http://www.mozilla.org/MPL/
7 Software distributed under the License is distributed on an "AS IS"
8 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9 License for the specific language governing rights and limitations
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU Lesser General Public license (the "LGPL License"), in which case the
14 provisions of LGPL License are applicable instead of those
17 For feedback and questions about my Files and Projects please mail me,
18 Alexander Matthes (Ziz) , zizsdl_at_googlemail.com
20 #define AHA_VERSION "0.4.8.0"
21 #define AHA_YEAR "2015"
29 int getNextChar(register FILE* fp
)
37 if ((c
= fgetc(fp
)) != EOF
)
39 fprintf(stderr
,"Unknown Error in File Parsing!\n");
43 typedef struct selem
*pelem
;
44 typedef struct selem
{
45 unsigned char digit
[8];
46 unsigned char digitcount
;
50 pelem
parseInsert(char* s
)
54 unsigned char digit
[8];
55 unsigned char digitcount
=0;
57 for (pos
=0;pos
<1024;pos
++)
61 if (s
[pos
]==';' || s
[pos
]==0)
69 pelem newelem
=(pelem
)malloc(sizeof(telem
));
70 for (unsigned char a
=0;a
<8;a
++)
71 newelem
->digit
[a
]=digit
[a
];
72 newelem
->digitcount
=digitcount
;
77 momelem
->next
=newelem
;
87 digit
[digitcount
]=s
[pos
]-'0';
94 void deleteParse(pelem elem
)
98 pelem temp
=elem
->next
;
104 #define VERSION_PRINTF_MAKRO \
105 printf("\033[1;31mAnsi Html Adapter\033[0m Version "AHA_VERSION"\n");
107 int main(int argc
,char* args
[])
110 register FILE *fp
= stdin
;
111 int colorshema
=0; //0:normal, 1:black, 2:pink
119 //Searching Parameters
120 for (int p
= 1;p
<argc
;p
++)
122 if ((strcmp(args
[p
],(char*)"--help")==0) || (strcmp(args
[p
],(char*)"-h")==0) || (strcmp(args
[p
],(char*)"-?")==0))
125 printf("\033[1maha\033[0m takes SGR-colored Input and prints W3C conform HTML-Code\n");
126 printf("use: \033[1maha\033[0m <\033[4moptions\033[0m> [\033[4m-f file\033[0m]\n");
127 printf(" \033[1maha\033[0m (\033[4m--help\033[0m|\033[4m-h\033[0m|\033[4m-?\033[0m)\n");
128 printf("\033[1maha\033[0m reads the Input from a file or stdin and writes HTML-Code to stdout\n");
129 printf("\033[4moptions\033[0m: --black, -b: \033[1;30m\033[1;47mBlack\033[0m Background and \033[1;37mWhite\033[0m \"standard color\"\n");
130 printf(" --pink, -p: \033[1;35mPink\033[0m Background\n");
131 printf(" --stylesheet, -s: Use a stylesheet instead of inline styles\n");
132 printf(" --iso X, -i X: Uses ISO 8859-X instead of utf-8. X must be 1..16\n");
133 printf(" --title X, -t X: Gives the html output the title \"X\" instead of\n");
134 printf(" \"stdin\" or the filename\n");
135 printf(" --line-fix, -l: Uses a fix for inputs using control sequences to\n");
136 printf(" change the cursor position like htop. It's a hot fix,\n");
137 printf(" it may not work with any program like htop. Example:\n");
138 printf(" \033[1mecho\033[0m q | \033[1mhtop\033[0m | \033[1maha\033[0m -l > htop.htm\n");
139 printf(" --word-wrap, -w: Wrap long lines in the html file. This works with\n");
140 printf(" CSS3 supporting browsers as well as many older ones.\n");
141 printf(" --no-header, -n: Don't include header into generated HTML,\n");
142 printf(" useful for inclusion in full HTML files.\n");
143 printf("Example: \033[1maha\033[0m --help | \033[1maha\033[0m --black > aha-help.htm\n");
144 printf(" Writes this help text to the file aha-help.htm\n\n");
145 printf("Copyleft \033[1;32mAlexander Matthes\033[0m aka \033[4mZiz\033[0m "AHA_YEAR
"\n");
146 printf(" \033[5;36mzizsdl@googlemail.com\033[0m\n");
147 printf(" \033[5;36mhttp://ziz.delphigl.com/tool_aha.php\033[0m\n");
148 printf("This application is subject to the \033[1;34mMPL\033[0m or \033[1;34mLGPL\033[0m.\n");
152 if ((strcmp(args
[p
],(char*)"--version")==0) || (strcmp(args
[p
],(char*)"-v")==0))
158 if ((strcmp(args
[p
],"--title")==0) || (strcmp(args
[p
],"-t")==0))
162 fprintf(stderr
,"No title given!\n");
169 if ((strcmp(args
[p
],"--line-fix")==0) || (strcmp(args
[p
],"-l")==0))
174 if ((strcmp(args
[p
],"--no-header")==0) || (strcmp(args
[p
],"-n")==0))
179 if ((strcmp(args
[p
],"--word-wrap")==0) || (strcmp(args
[p
],"-w")==0))
182 if ((strcmp(args
[p
],"--black")==0) || (strcmp(args
[p
],"-b")==0))
185 if ((strcmp(args
[p
],"--pink")==0) || (strcmp(args
[p
],"-p")==0))
188 if ((strcmp(args
[p
],"--stylesheet")==0) || (strcmp(args
[p
],"-s")==0))
191 if ((strcmp(args
[p
],"--iso")==0) || (strcmp(args
[p
],"-i")==0))
195 fprintf(stderr
,"No ISO code given!\n");
198 iso
= atoi(args
[p
+1]);
201 fprintf(stderr
,"not a valid ISO code: ISO 8859-%s\n",args
[p
+1]);
207 if (strcmp(args
[p
],"-f")==0)
211 fprintf(stderr
,"no file to read given after \"-f\"!\n");
214 fp
= fopen(args
[p
+1],"r");
217 fprintf(stderr
,"file \"%s\" not found!\n",args
[p
+1]);
225 fprintf(stderr
,"Unknown parameter \"%s\"\n",args
[p
]);
234 printf("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
236 printf("<?xml version=\"1.0\" encoding=\"ISO-8859-%i\" ?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n",iso
);
237 printf("<!-- This file was created with the aha Ansi HTML Adapter. http://ziz.delphigl.com/tool_aha.php -->\n");
238 printf("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
239 printf("<head>\n<meta http-equiv=\"Content-Type\" content=\"application/xml+xhtml; charset=UTF-8\" />\n");
241 printf("<title>%s</title>\n",title
);
245 printf("<title>stdin</title>\n");
247 printf("<title>%s</title>\n",filename
);
251 printf("<style type=\"text/css\">\n");
254 case 1: printf("body {color: white; background-color: black;}\n");
255 printf(".reset {color: white;}\n");
256 printf(".bg-reset {background-color: black;}\n");
258 case 2: printf("body {background-color: pink;}\n");
259 printf(".reset {color: black;}\n");
260 printf(".bg-reset {background-color: pink;}\n");
262 default: printf(".reset {color: black;}\n");
263 printf(".bg-reset {background-color: white;}\n");
267 printf(".dimgray {color: dimgray;}\n");
268 printf(".red {color: red;}\n");
269 printf(".green {color: green;}\n");
270 printf(".yellow {color: olive;}\n");
271 printf(".blue {color: blue;}\n");
272 printf(".purple {color: purple;}\n");
273 printf(".cyan {color: teal;}\n");
274 printf(".white {color: gray;}\n");
275 printf(".bg-black {background-color: black;}\n");
276 printf(".bg-red {background-color: red;}\n");
277 printf(".bg-green {background-color: green;}\n");
278 printf(".bg-yellow {background-color: olive;}\n");
279 printf(".bg-blue {background-color: blue;}\n");
280 printf(".bg-purple {background-color: purple;}\n");
281 printf(".bg-cyan {background-color: teal;}\n");
282 printf(".bg-white {background-color: gray;}\n");
286 printf(".dimgray {color: dimgray;}\n");
287 printf(".red {color: red;}\n");
288 printf(".green {color: lime;}\n");
289 printf(".yellow {color: yellow;}\n");
290 printf(".blue {color: #3333FF;}\n");
291 printf(".purple {color: fuchsia;}\n");
292 printf(".cyan {color: aqua;}\n");
293 printf(".white {color: white;}\n");
294 printf(".bg-black {background-color: black;}\n");
295 printf(".bg-red {background-color: red;}\n");
296 printf(".bg-green {background-color: lime;}\n");
297 printf(".bg-yellow {background-color: yellow;}\n");
298 printf(".bg-blue {background-color: #3333FF;}\n");
299 printf(".bg-purple {background-color: fuchsia;}\n");
300 printf(".bg-cyan {background-color: aqua;}\n");
301 printf(".bg-white {background-color: white;}\n");
303 printf(".underline {text-decoration: underline;}\n");
304 printf(".bold {font-weight: bold;}\n");
305 printf(".blink {text-decoration: blink;}\n");
306 printf("</style>\n");
310 printf("<style type=\"text/css\">pre {white-space: pre-wrap; white-space: -moz-pre-wrap !important;\n");
311 printf("white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;}</style>\n");
314 if (stylesheet
|| ! colorshema
)
320 case 1: printf("<body style=\"color:white; background-color:black\">\n"); break;
321 case 2: printf("<body style=\"background-color:pink\">\n"); break;
326 //printf("<div style=\"font-family:monospace; white-space:pre\">");
330 //Begin of Conversion
332 int fc
= -1; //Standard Foreground Color //IRC-Color+8
333 int bc
= -1; //Standard Background Color //IRC-Color+8
334 int ul
= 0; //Not underlined
335 int bo
= 0; //Not bold
336 int bl
= 0; //No Blinking
337 int ofc
,obc
,oul
,obo
,obl
; //old values
342 while ((c
=fgetc(fp
)) != EOF
)
352 //Searching the end (a letter) and safe the insert:
356 while ((c
<'A') || ((c
>'Z') && (c
<'a')) || (c
>'z'))
360 if (c
=='>') //end of htop
371 //printf("\n%s\n",buffer); //DEBUG
372 elem
=parseInsert(buffer
);
374 while (momelem
!=NULL
)
378 while (mompos
<momelem
->digitcount
&& momelem
->digit
[mompos
]==0)
380 if (mompos
==momelem
->digitcount
) //only zeros => delete all
382 bo
=0;ul
=0;bl
=0;fc
=-1;bc
=-1;
386 switch (momelem
->digit
[mompos
])
389 case 2: if (mompos
+1<momelem
->digitcount
)
390 switch (momelem
->digit
[mompos
+1])
392 case 1: //Reset blink and bold
396 case 4: //Reset underline
399 case 7: //Reset Inverted
401 if (fc
== -1 || fc
== 9)
410 if (temp
== -1 || temp
== 9)
422 case 3: if (mompos
+1<momelem
->digitcount
)
423 fc
=momelem
->digit
[mompos
+1];
425 case 4: if (mompos
+1==momelem
->digitcount
)
428 bc
=momelem
->digit
[mompos
+1];
431 case 7: //TODO: Inverse
433 if (fc
== -1 || fc
== 9)
442 if (temp
== -1 || temp
== 9)
454 momelem
=momelem
->next
;
459 if (htop_fix
) //a little dirty ...
461 elem
=parseInsert(buffer
);
462 pelem second
=elem
->next
;
465 newline
=second
->digit
[0]-1;
466 if (second
->digitcount
>1)
467 newline
=(newline
+1)*10+second
->digit
[1]-1;
468 if (second
->digitcount
>2)
469 newline
=(newline
+1)*10+second
->digit
[2]-1;
479 for (;line
<80;line
++)
482 //Checking the differences
483 if ((fc
!=ofc
) || (bc
!=obc
) || (ul
!=oul
) || (bo
!=obo
) || (bl
!=obl
)) //ANY Change
485 if ((ofc
!=-1) || (obc
!=-1) || (oul
!=0) || (obo
!=0) || (obl
!=0))
487 if ((fc
!=-1) || (bc
!=-1) || (ul
!=0) || (bo
!=0) || (bl
!=0))
490 printf("<span class=\"");
492 printf("<span style=\"");
495 case 0: if (stylesheet
)
498 printf("color:dimgray;");
500 case 1: if (stylesheet
)
503 printf("color:red;");
505 case 2: if (stylesheet
)
507 else if (colorshema
!=1)
508 printf("color:green;");
510 printf("color:lime;");
512 case 3: if (stylesheet
)
514 else if (colorshema
!=1)
515 printf("color:olive;");
517 printf("color:yellow;");
519 case 4: if (stylesheet
)
521 else if (colorshema
!=1)
522 printf("color:blue;");
524 printf("color:#3333FF;");
526 case 5: if (stylesheet
)
528 else if (colorshema
!=1)
529 printf("color:purple;");
531 printf("color:fuchsia;");
533 case 6: if (stylesheet
)
535 else if (colorshema
!=1)
536 printf("color:teal;");
538 printf("color:aqua;");
540 case 7: if (stylesheet
)
542 else if (colorshema
!=1)
543 printf("color:gray;");
545 printf("color:white;");
547 case 9: if (stylesheet
)
549 else if (colorshema
!=1)
550 printf("color:black;");
552 printf("color:white;");
557 case 0: if (stylesheet
)
560 printf("background-color:black;");
562 case 1: if (stylesheet
)
565 printf("background-color:red;");
567 case 2: if (stylesheet
)
569 else if (colorshema
!=1)
570 printf("background-color:green;");
572 printf("background-color:lime;");
574 case 3: if (stylesheet
)
575 printf("bg-yellow ");
576 else if (colorshema
!=1)
577 printf("background-color:olive;");
579 printf("background-color:yellow;");
581 case 4: if (stylesheet
)
583 else if (colorshema
!=1)
584 printf("background-color:blue;");
586 printf("background-color:#3333FF;");
588 case 5: if (stylesheet
)
589 printf("bg-purple ");
590 else if (colorshema
!=1)
591 printf("background-color:purple;");
593 printf("background-color:fuchsia;");
595 case 6: if (stylesheet
)
597 else if (colorshema
!=1)
598 printf("background-color:teal;");
600 printf("background-color:aqua;");
602 case 7: if (stylesheet
)
604 else if (colorshema
!=1)
605 printf("background-color:gray;");
607 printf("background-color:white;");
609 case 9: if (stylesheet
)
611 else if (colorshema
==1)
612 printf("background-color:black;");
613 else if (colorshema
==2)
614 printf("background-color:pink;");
616 printf("background-color:white;");
622 printf("underline ");
624 printf("text-decoration:underline;");
631 printf("font-weight:bold;");
638 printf("text-decoration:blink;");
646 if (c
==13 && htop_fix
)
648 for (;line
<80;line
++)
675 case '&': printf("&"); break;
676 case '\"': printf("""); break;
677 case '<': printf("<"); break;
678 case '>': printf(">"); break;
679 case '\n':case 13: momline
++;
681 default: printf("%c",c
);
683 if (iso
>0) //only at ISOS
684 if ((c
& 128)==128) //first bit set => there must be followbytes
691 for (int meow
=1;meow
<bits
;meow
++)
692 printf("%c",getNextChar(fp
));
699 if ((fc
!=-1) || (bc
!=-1) || (ul
!=0) || (bo
!=0) || (bl
!=0))