[英]text of a nested bullet-point list to HTML
我有這個:輸入示例:
* First item
* Second item
* Subitem 1
* sub-subitem!
* Subitem 3
* Third item
輸出示例:
<ul>
<li>First item</li>
<li>Second item
<ul>
<li>Subitem 1
<ul>
<li>sub-subitem!</li>
</ul>
</li>
<li>Subitem 3</li>
</ul>
</li>
<li>Third item</li>
</ul>
我創建了一個Java類,該類將每個String行發送到一個char數組,並且我單獨對待每個字符。 我的問題是何時關閉標簽?
這是我的代碼:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TextToHtml {
StringBuilder itemName = new StringBuilder();
String sCurrentLine;
int usingUlTAG=0;
public TextToHtml(){
BufferedReader br = null;
try {
boolean closeLItag=false;
br = new BufferedReader(new FileReader("NestedText.txt"));
System.out.println("<ul>");
while ((sCurrentLine = br.readLine()) != null) {
char[] item = sCurrentLine.toCharArray();
for(int i=0; i<item.length;i++){
if(item[i]!='*' && item[i]!='\n' && item[i]!='\t'){
itemName.append(item[i]);
continue;
}
if(item[i]=='*'){
itemName.append("<li>");
closeLItag=true;
}
else if(item[i]=='\t'){
if(item[i+1]=='*'){
if(usingUlTAG<1)
itemName.append("\t<ul>\n\t\t");
itemName.append("\t\n\t\t");
usingUlTAG= 1;
continue;
}
if(item[i+1]=='\t'){
itemName.append("\t\t<ul>\n\n\t\t");
usingUlTAG=2;
continue;
}
}
}
if(closeLItag){
itemName.append("</li>\n");
}
}
System.out.println(itemName+"/ul>");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new TextToHtml();
}
}
您必須先查看下一行,然后查看其列表級別是否與當前項目的列表級別不同。 然后,您可以根據級別差異(如果有)添加或關閉標簽。 這是執行此操作的代碼:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TextToHtml
{
StringBuilder itemName = new StringBuilder();
String sCurrentLine;
String sNextLine; // A "peek" at what's next to determine if </li> is needed
public TextToHtml()
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader("NestedText.txt"));
System.out.println("<ul>");
sNextLine = br.readLine();
while ((sCurrentLine = sNextLine) != null)
{
sNextLine = br.readLine();
char[] item = sCurrentLine.toCharArray();
int itemLevel = 0;
for (int i = 0; i < item.length; i++)
{
if (item[i] != '*' && item[i] != '\n' && item[i] != '\t')
{
itemName.append(item[i]);
}
else if (item[i] == '*')
{
itemName.append("\t<li>");
// Trim leading space character
if (item[i + 1] == ' ')
i++;
}
else if (item[i] == '\t')
{
itemLevel++;
itemName.append("\t\t");
}
}
int nextItemLevel = 0;
if (sNextLine != null)
{
char[] nextItem = sNextLine.toCharArray();
for (int i = 0; i < nextItem.length; i++)
{
if (nextItem[i] == '\t')
nextItemLevel++;
else
break;
}
}
// Next is the same level; there are no subitems
if (itemLevel == nextItemLevel)
itemName.append("</li>");
// Next is a deeper level; there are subitems
else if (itemLevel < nextItemLevel)
{
// In case the next item is more than 1 level deeper
for (int i = itemLevel + 1; i <= nextItemLevel; i++)
{
itemName.append("\n");
for (int j = 0; j < i; j++)
itemName.append("\t\t");
itemName.append("<ul>");
// If the next item's level is reached, it will create its own <li>
if (i != nextItemLevel)
{
itemName.append("\n");
for (int j = 0; j < i; j++)
itemName.append("\t\t");
itemName.append("\t<li>");
}
}
}
// Next is a higher level; there are tags to close
else // (itemLevel > nextItemLevel)
{
itemName.append("</li>");
for (int i = itemLevel - 1; i >= nextItemLevel; i--)
{
itemName.append("\n");
for (int j = 0; j <= i; j++)
itemName.append("\t\t");
itemName.append("</ul>\n");
for (int j = 0; j < i; j++)
itemName.append("\t\t");
itemName.append("\t</li>");
}
}
itemName.append("\n");
}
System.out.println(itemName + "</ul>");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
new TextToHtml();
}
}
請注意,這僅在級別使用制表符而不是空格縮進時才有效。
如果,按照您當前的代碼建議,如果原始文本中的列表項行都使用硬標簽進行縮進,那么您要做的就是一次一行地處理文本,並跟蹤縮進級別(標簽數) 上一行。 這段代碼不會在生成的HTML中產生好的縮進,但是它正確地嵌套了<ul>
和<li>
,這是HTML瀏覽器真正關心的所有問題
import java.io.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) throws Exception {
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader("NestedText.txt"));
try {
int lastIndent = -1; // indent level of last line
int depth = 0; // number of levels of <ul> we are currently inside
String line;
Pattern indentPattern = Pattern.compile("((\\t*)\\* )?(.*)");
while((line = br.readLine()) != null) {
Matcher m = indentPattern.matcher(line);
m.matches(); // guaranteed to be true, but needed to update matcher state
if(m.group(1) != null) { // this is a new list item
int thisIndent = m.end(2); // number of leading tabs, may be zero
// there are three possible cases
if(thisIndent == lastIndent) {
// same level as last list item
result.append("</li>");
} else if(thisIndent > lastIndent) {
// starting a child list
result.append("<ul>");
depth++;
} else {
// returning to parent list
result.append("</li>");
depth--;
result.append("</ul>");
result.append("</li>");
}
result.append("<li>");
lastIndent = thisIndent;
} else { // this is a continuation of the previous list item
result.append(" ");
}
// append this line's text (not including the indent and *)
result.append(m.group(3));
}
// run out of items, close any outstanding lists
while(depth-- > 0) {
result.append("</li>");
result.append("</ul>");
}
System.out.println(result);
} finally {
br.close();
}
}
}
在這里,我將任何以制表符和星號開頭的行都不是前一個<li>
的延續,即
* This is a very long list
item that continues over several
lines
* This is a second item
* this is a child item
that also continues
over several lines
還可以
我已根據Jan Dvorak的建議解決了此問題。 以下代碼適用於此,如果有幫助,我在下面說明。 感謝您的所有貢獻
MarkdownProcessor m = new MarkdownProcessor();
String html = null;
try {
html = m.markdown(MyString));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(html);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.