import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class HtmlFormatter {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java HtmlFormatter <input_file>");
return;
}
String inputFilePath = args[0];
try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(formatLine(line));
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
}
private static String formatLine(String line) {
// Basic indentation: indent after opening tags
if (line.contains("<")) {
int openTagIndex = line.indexOf("<");
if (openTagIndex != -1) {
// Find the closing tag
int closingTagIndex = line.indexOf(">", openTagIndex);
if (closingTagIndex != -1) {
String openingTag = line.substring(0, openTagIndex + 1);
String closingTag = line.substring(closingTagIndex);
//Indentation level
int indentationLevel = 0;
while (openingTag.charAt(indentationLevel) != '{') {
indentationLevel++;
}
String indentation = " ".repeat(indentationLevel);
return indentation + line;
} else {
return line; // If closing tag not found, return as is
}
}
}
return line; // Return unchanged if no HTML tags
}
}
Add your comment