• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a I/O method to convert text to html?  RSS feed

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a method to convert text to html? I'm trying to do this without using JSPs or Servlets. Pure java classes.
 
Sheriff
Posts: 21181
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure there is:


There you have it!

You think I'm kidding? Definitely not. After all, this is perfectly valid (wel ok, you could add the doctype, header, body tag etc) HTML.

The thing is, text does not have any real markup. You could read it all in, create a <p> block for every piece of code until an empty line is encountered, create ordered lists, unordered lists, create links for every URL in the text, but in the end, it will still look quite similar to the <pre> block.
 
Author and ninkuma
Marshal
Posts: 66345
155
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could it be expected to know what type of markup you want?
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Rob]: The thing is, text does not have any real markup. You could read it all in, create a <p> block for every piece of code until an empty line is encountered, create ordered lists, unordered lists, create links for every URL in the text, but in the end, it will still look quite similar to the <pre> block.

Well, it is possible to make a few substantial improvements. E.g. the <pre> block will prevent word wrapping, which could well be a problem for some documents. So replacing each line break with <br> or <p> could well be an improvement.

Anyway Jay, the thing is that there's no standard way to do this. There are a number of aesthetic choices to make, depending on the nature of the text you're dealing with. Your best bet may be to code up something that reads each line, loops through each character, converts special characters to escape sequences, and appaned <br/> at the end of each line. Maybe leading spaces on a line could become &nbsp;. Depending on how you like the results, you can modify as necessary.

Or you can google for"java "text to html"" to look for existing code to do this sort of thing. A couple possibilities are this Text2HTML filter, or MarkdownJ. I've never used either, and don't know how close they may be to what you want.
[ November 02, 2007: Message edited by: Jim Yingst ]
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok the best implementation seems to be to read in each line in the text file , then add the html tags to the front and end , then spit that content into another file and add the html extension.
I just thought they might be a java class or something that already did this, like htmlwriter or something
Anyone know how to retrieve files from the hard drive?
[ November 02, 2007: Message edited by: Jay Dilla ]
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, sounds like you just want plain text to look reasonably nice. Wrapping the whole thing with <html> and </html> is a good start.

The <pre> suggestion might be exactly right for you. That would preserve all the formatting in your text file, like indents and aligned columns. It's not the snazziest look, though.

If you rule out preformatted, do you want to keep the line breaks where they are now? Then you can just add <br> to the end of every line. That might wind up looking funny when the user resizes the browser.

If you want to call a block of text with no blank lines a paragraph and let the paragraph word-wrap and resize to the browser width, don't do the <br> bit. Instead replace a blank line with <p/> and let the rest flow right along with no tags.

Your idea of wrapping every line in some tag would be good for certain inputs. For example if you copy paragraphs from MS Word and paste them in a text editor, each paragraph will be one line, terminated by newline. For that, you could just wrap each line with <p> and <p/> and be done.

If you can put some minimal markup in the text you can borrow Wiki ideas and generate headings and lists with bullets or numbers pretty easily.

Do all those options make sense? Any of them sound right?
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a next line type method for FileWriter? I am able to copy from one file to another but I can format it correctly using filewriter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!