Write a program to convert a text file into html file with questions and collapsible answers.
I have a text file which has questions and answers. The file was extracted as a csv file from an sqlite database with ^^^ as a row separator and | as a column separator.
I read the file using nio Files. readAllBytes() method.
Path filepath = Paths.get(filename);
try {
byte[] byteArray = Files.readAllBytes(filepath);
String str = new String(byteArray);
}catch(IOException e){
System.out.println(e.getMessage());
}
As the method may throw IOException, I have wrapped it in try block. The method reads the entire file and stores it in a bytearray which is conv erted to string.
Next to split the content into individual questions, I use string.split(regex) method. This method splits the string into an array, breaking the string at the occurrence of regex.
Unfortunately, regex has special meaning for ^ symbol. To use ^^^ as a separator, I have to escape this.
String arr[] = str.split("\\^\\^\\^");
Next in a for each loop, I take one question and split it into question and answer using escaped vertical tab as separator.
for(String qn:arr){
if(qn.trim()=="")
continue;
String qnans[] = qn.split("\\|");
.....
}
That's all. Now what is remaining is adding HTML tags. I got a javascript to display a collapsible content which should be included in the HTML body tag.
The code uses buttons of class accordion and hides/shows the adjacent the content.
Here is the complete for loop which does that.
for(String qn:arr){
if(qn.trim()=="")
continue;
String qnans[] = qn.split("\\|");
writer.write("<hr color='#eee' >");//this will add a horizontal separator
writer.write(qnans[0]);//question
writer.write("<button class='accordion'>Answer < button>");//button
writer.write("<div class='panel'>");//collapsible content
writer.write(qnans[1]);//answer
writer.write("<div>");
}
Here is the complete program
import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; import java.io.IOException; import java.io.FileWriter; public class ReadToHTML{ public static void writeHtml(String str,String filename) throws IOException{ FileWriter writer = new FileWriter(filename); str = str.trim(); writer.write("<html><head><meta charset='utf-8' /><title></title><style>.accordion { background-color: #eee; color: #444; cursor: pointer; padding: 10px; width: auto; border:none; text-align: left; outline: none; font-size: 15px; transition: 0.4s;}.active, .accordion:hover { background-color: #ccc; }.panel { padding: 0 18px; display: none; background-color: #ddd; overflow: hidden;}</style></head><body >"); String arr[] = str.split("\\^\\^\\^"); System.out.println("The number of questions is "+arr.length); for(String qn:arr){ if(qn.trim()=="") continue; String qnans[] = qn.split("\\|"); writer.write("<hr color='#eee'>\n<p>"); writer.write(qnans[0]); writer.write("</p>"); writer.write("<button class='accordion'>Answer</button>"); writer.write("<div class='panel'>"); if(qnans.length<2) continue; System.out.println("Question is "+qnans[0]); System.out.println("Answer is "+qnans[1]); writer.write("<p >"); writer.write(qnans[1]); writer.write("</p>"); writer.write("</div>"); } writer.write("<script >var acc = document.getElementsByClassName(\"accordion\");var i;for (i = 0; i < acc.length; i++) { acc[i].addEventListener(\"click\", function() { /* Toggle between adding and removing the active class, to highlight the button that controls the panel */ this.classList.toggle(\"active\"); /* Toggle between hiding and showing the active panel */ var panel = this.nextElementSibling; if (panel.style.display === \"block\") { panel.style.display = \"none\"; } else { panel.style.display = \"block\"; } });} </script></body></html>"); writer.close(); } public static void main(String args[]){ if(args.length<1) return; String filename = args[0]; Path filepath = Paths.get(filename); try { byte[] byteArray = Files.readAllBytes(filepath); String str = new String(byteArray); System.out.println("The string is "+str); writeHtml(str,args[1]); }catch(IOException e){ System.out.println(e.getMessage()); } } }
Comments
Post a Comment