JAVA 2 HTML:
import java.io.*;
import java.io.*;
class c2h {
static int maxKeyWords = 10000; //max
number of keywords per language
//ini variables
static String space = " ";
static String outFile =
"outFile.html";
static String iniFile =
"c2h.ini";
static String ftypeExt =
"filetype";
static String languageName =
"language";
static String numbers =
"%NUMBERS%";
static String background =
"%BACKGROUND%";
static String text = "%TEXT%";
static String end =
"<end>";
static String commentary = "//";
static String t2start = "|", t2end = "|"; //Keyword type 2,
starts with/ends with;
static String t3start = "[", t3end = "]"; //Keyword type 3,
starts with/ends with;
static String t4start = "{", t4end = "}"; //Keyword type 4,
starts with/ends with;
//html-tags
static String htmlStart =
"<html>", htmlEnd =
"</html>";
static String preformatertStart =
"<pre>", preformatertEnd
= "</pre>";
static String bodyStart =
"<body>", bodyEnd =
"</body>";
static String fontStart = "<font
color=", fontEnd =
"</font>";
static String codeFile = "", ext = "", Sprak = "", left = "", right = "";
static String backCol =
"#FFFFFF", numbCol =
"#CC0000", textCol =
"#000000"; //default colors
//here the keywords are
stored
//[keyword
nr.][keyword,type,color]
//type:1=regular
keyword,2="line" starts with,3=string start and stops with
//4=start and stops with,
over several lines possibly
static String keyWord[][] = new
String[maxKeyWords][3];//
static String fext[] = new String[100];
static String temp[] = new String[100];
static int counter = 0, flin = 0; //count everytime I read the ini
file to print line number with error
//global usage variables
static String s = "", le = "", ri = "", mi = "";
static int l = 0, i = 0,
i2 = 0;
public static void main(String[] args)
throws Exception {
System.out.println("\n-------------------------------------");
System.out.println("Code2HTML v1.0
by Kenneth Sundnes");
System.out.println("kenneths@stud.cs.uit.no IFI-UiT-2002");
System.out.println("-------------------------------------\n");
if (fileExist(iniFile) == false) {
iniError();
}
if (args.length == 0) {
noFileError();
}
if (fileExist(args[0]) == false) {
fileError(args[0]);
}
ext = getExtension(args[0]);
codeFile = args[0];
try {
readIniFile(); //read ini file
findOutFile(); //figure output
outFilename
htmlOut(); //write the html!
}
catch (Exception exp) {
System.out.println(exp);
System.out.println("Something
wrong happened :-(, the file " + outFile + " is probably wrongly
converted.");
System.exit(0);
}
if (fileExist(outFile) == false) {
System.out.println("Something
wrong happened :-(, the file " + outFile + " is NOT written!");
} else {
System.out.println("The file
" + outFile + " is now written!");
}
}
//Reads the INI file
static void readIniFile() throws Exception
{
String t = "", ftemp =
"";
BufferedReader fin =
fileOpenRead(iniFile);
for (;;) {
t = readLineFromFile(fin).trim();
//read a line from ini file
if (t == null) {
break; //if eof - out of the
loop
}
for (;;) {
storeExt(getSubstring(ftypeExt,
fin)); //finds a line with filtype in it
if (stringInArray(fext, ext) !=
-1) { //if the input files extension matches this one:
String b =
getSubstring(languageName, fin); //finds next line with "language"
if (b == null) {
errorInIni(); //if EOF
- out!
}
if
(b.indexOf("=") == -1) {
errorInIni(); //if the
line does not have '=' - out!
}
Sprak =
b.substring(b.indexOf("=") + 1); //stores language name
for (;;) {
b = nextLine(fin);
//gets next line
if (b == null) {
errorInIni(); //if
eof error
}
if (b.indexOf(end) !=
-1) {
break; //if
<end> then finished
}
if
(b.indexOf("<#") == -1) { //if its not a <color>
if (ftemp ==
"") {
errorInIni();
//if color is blank - out , error
}
temp =
words2Array(b, 0); //stores this lines keywords in an array
for (int x = 0; x
< temp.length; x++) { //for each keyword
temp[x] =
replaceString(temp[x], "<", "<", false); //'<'
is readied for html
temp[x] =
replaceString(temp[x], ">", ">", false); //'>'
is readied for html
if (temp[x].indexOf(background) != -1) {
backCol =
ftemp;
} else if
(temp[x].indexOf(numbers) != -1) {
numbCol =
ftemp;
} else if (temp[x].indexOf(text) != -1) {
textCol =
ftemp;
} else {
keyWord[counter][0] = temp[x]; //stores the keyword
keyWord[counter][1] =
int2String(keyType(temp[x]));
keyWord[counter][2] = ftemp;
counter +=
1;
}
}
} else {
ftemp =
b.substring(1, b.length() - 1);
}
}
if (ftemp != "")
{
break;
}
}
if (ftemp != "") {
break;
}
}
if (ftemp != "") {
break;
}
}
}
//writes the HTML file
static void htmlOut() throws Exception {
BufferedWriter fout =
fileOpenWrite(outFile);
BufferedReader fin =
fileOpenRead(codeFile);
writeLineToFile(fout, htmlStart, 1);
writeLineToFile(fout,
"<head><title>Code2Html v1.0 - " + codeFile +
"</title></head>", 1);
writeLineToFile(fout, "<body
text=\"" + textCol + "\" bgcolor=\"" + backCol +
"\">" + preformatertStart + bodyStart, 1);
writeLineToFile(fout,
"<strong><font size='6'>" + codeFile +
"</font></strong><hr>", 1);
writeLineToFile(fout, "", 1);
for (;;) {
s = readLineFromFile(fin); //reads
a line from input file
if (s == null) {
break; //finished!
}
s = replaceString(s, "" +
(char) (byte) 9, " ", true); //replaces TAB's with 4 spaces
s = replaceString(s,
"<", "<", false); //HTML readied
s = replaceString(s,
">", ">", false); //HTML readied
writeLineToFile(fout,
colorNumbers(k1(k3(k2(k4(s, fin, fout))))), 1);
}
//writes out the end tags
writeLineToFile(fout, "", 1);
writeLineToFile(fout,
"<hr><font size='2'><i> Generated by
Code2Html - <a href=mailto:kenneths@stud.cs.uit.no>Kenneth
Sundnes</a> (C) 1999</i></font>", 1);
writeLineToFile(fout, bodyEnd +
preformatertEnd + htmlEnd, 1);
fout.close();
}
//returns a string with
all type 1 keywords colored
static String k1(String s) {
String t = "";
for (int x = 0; x < counter; x++) {
//if type 1
i2 = 0;
if (keyWord[x][1].equals("1")
== true) {
for (;;) {
t = "";
left = keyWord[x][0];
l = left.length();
i = s.indexOf(left, i2);
if (i == -1) {
break;
}
if (i == 0) {
t = " " +
s.substring(i, l + i);
} else {
t = s.substring(i - 1,
l + i);
}
if (s.length() == i + l) {
t += " ";
} else {
t += s.charAt(i + l);
}
if (keyword(t) != false | l
== 1) {
if (!isColored(s, i)
&& !isATag(s, i)) {
s = s.substring(0,
i) + fontStart + keyWord[x][2] + ">" + s.substring(i, i + l) +
fontEnd + s.substring(i + l);
i2 = i + 27 + left.length();
} else {
i2 = i +
left.length();
}
} else {
i2 = i + left.length();
}
}
}
}
return s;
}
//returns a string with
all type 2 keywords colored
static String k2(String s) {
for (int x = 0; x < counter; x++) {
//if type 2
if
(keyWord[x][1].equals("2") == true) {
left =
keyWord[x][0].substring(1, keyWord[x][0].length() - 1);
i = s.indexOf(left);
if (i != -1) {
if (commentaryInString(s,
i) == false) {
if (!isColored(s, i)
&& !isATag(s, i)) {
s = s.substring(0,
i) + fontStart + keyWord[x][2] + ">" + s.substring(i) + fontEnd;
}
}
}
}
}
return s;
}
//returns a string with
all type 4 keywords colored
static String k4(String s, BufferedReader
fin, BufferedWriter fout) throws Exception {
for (int x = 0; x < counter; x++) {
//if type = 4
if
(keyWord[x][1].equals("4") == true) {
left = keyWord[x][0].substring(1,
keyWord[x][0].indexOf(","));
right =
keyWord[x][0].substring(keyWord[x][0].indexOf(",") + 1,
keyWord[x][0].length() - 1);
i = s.indexOf(left);
i2 = s.indexOf(right, i + left.length());
l = left.length();
if (i == -1) {
break;
}
if (commentaryInString(s, i) ==
true) {
break;
}
if (i2 != -1) {
s = s.substring(0, i) +
fontStart + keyWord[x][2] + ">" + s.substring(i, i2 +
right.length()) + fontEnd + s.substring(i2 + right.length());
break;
}
s = s.substring(0, i) +
fontStart + keyWord[x][2] + ">" + s.substring(i);
writeLineToFile(fout, s, 1);
for (;;) {
s = readLineFromFile(fin);
s = replaceString(s,
"<", "<", false); //HTML readying
s = replaceString(s,
">", ">", false); //HTML readying
s = replaceString(s,
"" + (char) (byte) 9, " ", true); //replaces TAB's with 4
spaces
i = s.indexOf(right);
if (i != -1) {
if
(commentaryInString(s, i) != true) {
s = s.substring(0,
i + right.length()) + fontEnd + s.substring(i + right.length());
break;
}
}
writeLineToFile(fout, s,
1);
}
}
}
return s;
}
//returns a string with
all type 3 keywords colored
static String k3(String s) {
for (int x = 0; x < counter; x++) {
//if type 3
if
(keyWord[x][1].equals("3") == true) {
int y = 0;
left =
keyWord[x][0].substring(1, keyWord[x][0].indexOf(","));
right = keyWord[x][0].substring(keyWord[x][0].indexOf(",")
+ 1, keyWord[x][0].length() - 1);
l = left.length();
i2 = 0;
for (;;) {
i = s.indexOf(left, i2);
if (isColored(s, i) |
isATag(s, i)) {
break;
}
i2 = s.indexOf(right, i +
1);
if (i == -1 | i2 == -1) {
break; //keyword doesnt
exist break
}
le = s.substring(0, i + l);
//left part
mi = fontStart +
keyWord[x][2] + ">" + s.substring(i + l, i2) + fontEnd; //colored
part
ri = s.substring(i2);
//right part
i2 += 27 + left.length();
//next search position
s = le + mi + ri; //string
colored
}
}
}
return s;
}
//dont remember what this
does :(
static boolean keyword(String s) {
if (s.length();
)
return false;
byte b = (byte) s.charAt(0); //first
char
if (byte2Int(b) != -1) {
return false; //A number return
false
}
if (b < 91 & b > 64) {
return false; //If UCase letter
(ASCII value) return false
}
if (b < 123 & b > 96) {
return false; //If LCase letter
(ASCII value) return false
}
byte b2 = (byte) s.charAt(s.length() -
1); //same for last char
if (byte2Int(b2) != -1) {
return false;
}
if (b2 < 91 & b2 > 64) {
return false;
}
if (b2 < 123 & b2 > 96) {
return false;
}
return true; //passed all tests - true
}
//Colors all numbers in a
string, unless they are colored before
static String colorNumbers(String s) {
char a;
int start, end, ok = 0, x, y, yes = 0;
for (x = 0; x < s.length(); x++) {
if (isANumber(s.charAt(x))) {
yes = 0; //error check variable
if (x != 0) {
if (isANumber(s.charAt(x -
1)) && !isColored(s, x - 1)) {
yes++; //if a number in
front, and its not colored. no coloring.
}
if (isALetter(s.charAt(x -
1))) {
yes++; //if char before
is a letter. no coloring.
}
}
if (isColored(s, x)) {
yes++; //if already
colored. no coloring.
}
if (x != s.length() - 1
&& isALetter(s.charAt(x + 1))) {
yes++;
}
if (yes == 0) { //if yes
variable still is 0 we passed the test: color!
start = x;
for (y = x + 1; y <
s.length(); y++) //find end of number
{
if
(!isANumber(s.charAt(y))) {
break;
}
}
end = y - 1;
ok = 0;
if (start != 0) {
if
(isALetter(s.charAt(start - 1))) {
ok++;
}
}
if (end + 1 != s.length())
{
if
(isALetter(s.charAt(end + 1))) {
ok++;
}
}
if (ok == 0 &&
!isColored(s, start) && !isATag(s, start)) { //passed tests : color
s = s.substring(0,
start) + fontStart + numbCol + ">" + s.substring(start, end + 1) +
fontEnd + s.substring(end + 1);
x += 28 + (end -
start);
}
}
}
}
return s;
}
//check if pos in string
is inside a tag, true or false
static boolean isATag(String s, int pos) {
if (pos < 0) {
return false;
}
for (int x = pos; x < s.length();
x++) {
if (s.charAt(x) == '<') {
return false;
}
if (s.charAt(x) == '>') {
return true;
}
}
return false;
}
//is this byte a letter?
static boolean isALetter(char s) {
byte b = (byte) s;
if (b > 64 && b < 91) {
return true;
}
if (b > 96 && b < 123) {
return true;
}
if (b == 95) {
return true; // _ is accepted
numberPal.
}
return false;
}
//replaces all instances
of a string in a string with anohter string. mod bool is for TAB replace only.
static String replaceString(String s,
String fra, String til, boolean mod) {
int y = 0, l = til.length();
for (;;) {
int x = s.indexOf(fra, y);
if (x == -1) {
return s;
}
if (mod) { //tab replace.
String til2 = til;
int b = (x + 1) % 4;
if (b != 0) {
til2 = emptyString(4 - b);
}
s = s.substring(0, x) + til2 +
s.substring(x + fra.length());
y = x + til2.length();
} else { //regular replace
if (x == -1) {
return s;
}
s = s.substring(0, x) + til +
s.substring(x + fra.length());
y = x + l + 1;
}
}
}
//return an empty string
with y spaces
static String emptyString(int y) {
String a = "";
for (int x = 0; x < y; x++) {
a += ' ';
}
return a;
}
//Chekcs if a keyword is
already colored
static boolean isColored(String s, int pos)
{
int a = countString(fontStart, s, 0,
pos);
int b = countString(fontEnd, s, 0,
pos);
if (a > b) {
return true;
}
return false;
}
//returns how many times
a string a is in b from pos x to y
static int countString(String a, String b,
int x, int y) {
int z = 0;
while (true) {
x = b.indexOf(a, x);
if (x == -1 || x > y) {
return z;
}
z++;
x += a.length();
if (x >= y) {
break;
}
}
return z;
}
//Checks if a keyword
type 3 is part of type 2 and should not be colored
static boolean commentaryInString(String s,
int pos) {
int t = 0, l = 0, a = 0, b = 0;
String k = "", le =
"", ri = "";
for (int x = 0; x < counter; x++) {
if
(keyWord[x][1].equals("3") == true) {
k = keyWord[x][0];
l = k.length();
le = keyWord[x][0].substring(1,
k.indexOf(","));
ri =
keyWord[x][0].substring(k.indexOf(",") + 1, l - 1);
a = s.indexOf(le);
b = s.indexOf(ri, a +
le.length());
if (a != -1 & b != -1) {
if (a < pos & b >
pos) {
t = 1;
}
}
}
}
if (t == 0) {
return false;
} else {
return true;
}
}
//gets extension of a
filename in uppercase
static String getExtension(String fil) {
if (fil.indexOf(".") == 0) {
return null;
}
return
fil.substring(fil.indexOf(".") + 1).toUpperCase().trim();
}
//finds output filename.
If file exists. file2 will be written, and so on
static void findOutFile() {
String s = "", s2 =
"";
int a =
codeFile.indexOf(".");
s2 = codeFile.substring(0, a);
s = s2 + ".html";
outFile = s;
if (fileExist(s) == true) {
System.out.println("The file
" + s + " already exists.");
for (int x = 2; x < 100; x++) {
s = s2 + int2String(x) +
".html";
outFile = s;
if (fileExist(s) == false) {
break;
}
}
}
}
//stores possible
extensions for this language in this array
static void storeExt(String s) {
if (s.indexOf("=") == -1) {
errorInIni();
}
s =
s.substring(s.indexOf("=") + 1);
fext = words2Array(s, 1);
}
//returns which keytype
this keyword is
static int keyType(String s) {
if (s.startsWith(t2start) == true &
s.endsWith(t2end) == true) {
return 2;
}
if (s.startsWith(t3start) == true &
s.endsWith(t3end) == true) {
return 3;
}
if (s.startsWith(t4start) == true &
s.endsWith(t4end) == true) {
return 4;
}
return 1;
}
//Searches for a string
in a line in a file and return line when found or null if not found
static String getSubstring(String sok, BufferedReader
fin) throws Exception {
for (;;) {
String s =
readLineFromFile(fin).trim();
if (s != null & s.length() !=
0) {
if (s.indexOf(sok) != -1) {
return s;
}
}
if (s == null) {
return null;
}
}
}
//returns an array with
all the words in a string in each index
//type=0=same
case,1=ucase,2=lcase
static String[] words2Array(String s, int
stype) {
s = s.trim();
if (s == null | s.length() == 0) {
return null;
}
String t[] = new String[200];
if (stype == 1) {
s = s.toUpperCase();
}
if (stype == 2) {
s = s.toLowerCase();
}
for (int x = 0;; x++) {
if (s.indexOf(" ") == -1)
{
t[x] = s;
break;
}
t[x] = s.substring(0,
s.indexOf(" "));
s = s.substring(s.indexOf("
"));
s = s.trim();
}
String t2[] = new
String[arrayContentLength(t)];
for (int x = 0; x < t2.length; x++)
{
t2[x] = t[x];
}
return t2;
}
//Checks if a file exists
static boolean fileExist(String fil) {
File f = new File(fil);
return f.canRead();
}
//Returns a line from a
file which is not empty or a commentary (ini file)
static String nextLine(BufferedReader fin)
throws Exception {
String b = "";
for (;;) {
b = readLineFromFile(fin);
if (b == null) {
return null;
}
b = b.trim();
if (b.startsWith(commentary) ==
false & b.length() != 0) {
return b;
}
}
}
//opens a file for
reading
static BufferedReader fileOpenRead(String
Fname) throws IOException {
BufferedReader fin = new
BufferedReader(new FileReader(Fname));
return fin;
}
//opens a file for
writing
static BufferedWriter fileOpenWrite(String
Fname) throws Exception {
BufferedWriter fout = new
BufferedWriter(new FileWriter(Fname));
return fout;
}
//reads a line from file
static String readLineFromFile(BufferedReader
fin) throws Exception {
flin += 1;
return fin.readLine();
}
//writes a line to a
file, if lin=1 lineshift is added
static void writeLineToFile(BufferedWriter
fout, String s, int lin) throws Exception {
fout.write(space + s, 0, s.length() +
space.length());
if (lin == 1) {
fout.write(13);
fout.write(10);
}
}
//converts a ascii number
byte to an int number
static int byte2Int(byte b) {
if (b > 57 | b < 48) {
return -1;
}
return b - 48;
}
//converts an int to a
string
static String int2String(int tall) {
return String.valueOf(tall);
}
//returns
"real" length of a string array. not counting empty indexes
static int arrayContentLength(String[] s) {
int temp = 0;
for (int x = 0; x < s.length; x++) {
if (s[x] != null) {
temp = x;
}
}
return temp + 1;
}
//searches for a string
in an array, returns index if found, -1 if not found
static int stringInArray(String[] a, String
s) {
for (int x = 0; x < a.length; x++) {
if (a[x].indexOf(s) != -1) {
return x;
}
}
return -1;
}
//is this char a number?
static boolean isANumber(char s) {
byte b = (byte) s;
if (b > 57 | b < 48) {
return false;
}
return true;
}
//If no input file is
given output this:
static void noFileError() {
System.out.println("This program
converts text/sourcecode to syntax-highlighted HTML.");
System.out.println("Please refer
to c2h.ini for configuration/adding new languages.");
System.out.println();
System.out.println("You have to
specify a file to convert...");
System.out.println("I.e. java c2h
program.java");
System.out.println();
System.out.println("NB! If the
file name.html exists, name2.html will be created, and so on.");
System.exit(0);
}
//if ini file does not
exist
static void iniError() {
System.out.println("The file
" + iniFile + " does not exist. \nPlease contact author for new
file.");
System.exit(0);
}
//input file does not
exist
static void fileError(String fil) {
System.out.println("The file
" + fil + " does not exist.");
System.exit(0);
}
//error in ini file
static void errorInIni() {
System.out.println("There is an
error in the INI file.");
System.out.println("Check line:
" + flin);
}
}
It may be any programming language code which you can input, it will give formatted output as well.
ReplyDelete