开发者

How to write a parser for Markup?

I would like to program a parser for a Markup language similar to BBCode, Markdown, Wikisyntax etc. using a high-level language like Python or Perl. It should feature sectioning, code highlighting, automatic link creation, embedding images but allowing HTML for more complex formatting.

Has anyone done similar things or has worked closely with those systems and could des开发者_如何学JAVAcribe generally how this could be done efficiently? Although efficiency is not really of concern for such a small system, it is generally favourable. In particular I would like to learn if there is a more efficient way than using regular expressions for such a program.


For your general discussion…

You should start with the following blueprint:

  • you need to iterate charwise over entire data
  • you need to identify every char by its context, for it may be a tag-opening ('<', '[' etc) or just the char. This may be done by having an escapement flag, triggered by an escape-char (like backslashes in some languages do). if you use that approach, you also need to check for an escaped escapement.
  • you may also need some flag telling you to be inside a comment or special data section, that may have different escapement rules.
  • you need to build a tree-like structure or at least some stack for nested tags. This is why regexes are a bad idea: they not only take much to much overhead, they're also of no use if you want to get the correct closing tag for the second x (x=any tag) in the following snipped: <x><x><x></x><x><x></x></x><x></x><!-- </x> -->this one →</x><x></x></x>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜