示例编辑器 - 与 stackOverflow 相同

发布于 2024-10-25 00:11:04 字数 464 浏览 4 评论 0 原文

我想创建一个编辑器并将格式化文本存储在数据库中。我想要一个示例编辑器执行类似 StackOverFlow 编辑器的功能:

_sfdfdgdfgfg_ :对于下划线文本

/sfdfdgdfgfg/ :对于粗体文本,

我将使用一个函数来替换第一个 _ 和第二个 (分别为 /)。

所以我的问题是如何检测结尾和最后一个 _/ 如果它们是嵌套的?

例如:

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

我将在 Java 应用程序中使用这个编辑器。

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 editor in Java Application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

欲拥i 2024-11-01 00:11:04

因此,您需要的是 markdown 的 Java 版本。

以下是 Google 发现的内容:

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

So what you want is a Java Version of markdown.

Here's what Google finds:

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

骑趴 2024-11-01 00:11:04

它不会让你高兴,但你可能应该花时间学习编写解析器(龙书对此很好)。解析器任务的问题在于,如果您知道如何做,它们就很容易;如果您不知道,则几乎不可能。
我会编写一个标记生成器,可以识别 等标记作为您想要的格式指示符在您的编辑器中使用,并用于其他一切。结果可能如下所示:

文本:Hello _world_, /how are you?/

标记:,,< ;文本,“世界”>,,<文本,“,”>,,,,

使用布尔变量可以相当容易地跟踪开始和结束,因为它没有意义给它们筑巢。这就是为什么我已经在标记器中进行跟踪的原因。

之后,我将编写一个解析器类,它接受这些标记并相应地将输出配置到文本区域。

你看,这实际上只是分而治之原则的一个应用。 How do I do everything I Want with my text? 的任务分为 3 个部分:

  1. 根据一个有用的结构,这个字符串是关于什么的? (Tokenizer 的回答)
  2. 如何处理所有可能的 x 的特定文本部分 x? (解析器的回答)
  3. 我如何表示解析器对该字符串的解释? (JTextpane 或类似人员的回答)

Tokenizer 和 Parser 都不需要是额外的类。因为上下文并不复杂,所以它们可以只是您喜欢的 Textarea 类型的扩展类中的方法。

我认为提供更详细的建议是没有帮助的,下一个最佳步骤是您可能最好自己完成的实施。不过,如果您未能找到针对某个特定部分的良好解决方案,请毫不犹豫地询问。

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.

你在看孤独的风景 2024-11-01 00:11:04

您可以查看 stackoverflow.com 页面源代码并尝试集成...我想它应该可以工作...

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

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/

秋日私语 2024-11-01 00:11:04

这是一个展示如何使用 MarkDownJ 的示例:

首先,确保 MarkdownJ 作为 Java 应用程序中调用的类库。

其次,使用此代码调用 MarkdownJ :

MarkdownProcessor m = new MarkdownProcessor(); 

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

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

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>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文