开发者

sample editor - same as stackOverflow

I want to create an editor and store formatted text in database. I want just a sample editor do functions like StackOverFlow editor:

_sfdfdgdfgfg_ : for underlined text

/sfdfdgdfgfg/ : for bolded text

I will use a function to replace the first _ by <b> and for the second </b> (respec. for /).

So my question is how can I do to detect the end and the last _ and / if they are nested??

For example :

 /dsdfdfffddf _ dsdsdssd_/ ffdfddsgds /dfdfehgiuyhbf/  ....

I will use this edito开发者_开发百科r in Java Application.


So what you want is a Java Version of markdown.

Here's what Google finds:

http://code.google.com/p/markdownj/


It will not make you happy, but you should probably take the time to learn to write parsers (dragon book is nice for that). The thing with parser tasks is that they are easy if you know how to do it and nearly impossible if you don't. I would write a tokenizer that can recognize tokens like <start_underline, "_"> and <end_underline, "_"> for the format indicators you want to use in your editor and one for everything else. Results could look like this:

Text: Hello _world_, /how are you?/

Tokens: <text, "Hello ">,<start_underline, "_">,<text, "world">,<end_underline, "_">,<text, ", ">,<start_bold, "/">,<text, "how are you?">,<end_bold, "/">,

Start and End can be tracked fairly easy with boolean variables, because it makes no sense to nest them. That's why I would do that tracking in the tokenizer already.

After that I would write a parser class that takes these tokens and configures the output to a textarea accordingly.

You see, that this is actually just an application of the principle divide and conquer. The task of How do I do everything I want with my text? is split up into 3 parts:

  1. According to a useful structure, what is this string about? (Answer from Tokenizer)
  2. How do I handle specific textpart x for all possible x? (Answer from Parser)
  3. How do I represent the parsers interpretation of this string? (Answer from JTextpane or alike)

Both Tokenizer and Parser don't need to be extra classes. Because the context is not complicated they can just be methods in an extension class of the Textarea type you prefer.

Giving more detailed advice is not helpful I think, the next best step would be an implementation that you probably better want to do by yourself. Don't hesitate to ask if you fail to find a good solution to one specific part, though.


You can see stackoverflow.com Page Source and try to integrate... I guess it should work...

https://blog.stackoverflow.com/2008/12/reverse-engineering-the-wmd-editor/


This is an example show how to use MarkDownJ:

First, make sure that MarkdownJ is as a class library invoked in your Java application.

Second, use this code to invoke MarkdownJ :

MarkdownProcessor m = new MarkdownProcessor(); 

String html = m.markdown("this is *a sample text*");

System.out.print("<html>"+html+"</html>");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜