Eclipse oxygen 内置 markdown 语法(源码-简单分析)

在最近安装了Eclipse oxygen,默认支持Markdown编写,就写了个md测试文档。结果,md的语法有一些居然不支持,在网上查找没有找到eclipse的默认插件说明。ps:也可能是我的方法不对。

在这里先放出本次操作软件的相关信息:

eclipse软件信息:Eclipse Platform Oxygen (4.7)     Build id: I20170612-0950

JAVA Decomplier(class反编译):JD-GUI 0.3.5    JD-Core 0.6.2

Luyten(class反编译,支持jdk8,并且在GitHub有源码):0.5.3

解释一下为什么要用两个反编译工具。

jd反编译,优点:可以打开多个jar,多个jar互为依赖时,可以快捷跳转,搜索功能很强大。缺点:不支持jdk最新的语法,好像还报错,只能查看class、properties等几种格式文档预览,好像不再更新了

Luyten反编译,优点:开源,有EXE、jar两种执行包,支持多种格式文档的预览,支持最新的jdk语法,代码预览主题样式可变换,持续更新中,等功能。缺点:不支持同时打开多个,不可以代码快捷跳转,跟踪java代码不方便。

同时都不没有中文汉化,luyten可以尝试本地汉化。

网络资料:

markdown文件的基本常用编写语法(图文并茂)

XML中的 "<" ">" "&" 怎么转义

Luyten源码

在Eclipse中结合Ant自动化帮助文档

使用Eclipse写帮助文档

开始放出eclipse内置Markdown的源码,通过源码解读支持的语法。

jar包是在eclipse安装根路径下的plugins文件夹内:

wikitext插件包

发现了两个核心jar包和一个菜单UI包:


org.eclipse.mylyn.wikitext.markdown_3.0.6.20170311142502.jar

org.eclipse.mylyn.wikitext.markdown.ui_3.0.6.201703111926.jar

org.eclipse.mylyn.wikitext.ui_3.0.6.201703111926.jar


markdown.ui的包,内容比较少

目录结构

MarkDown.md,实例文件

### Markdown Markup Cheat Sheet

#### Phrase Modifiers

\`\`inline code\`\` - rendered as ``inline code``  
\`inline code\` - rendered as `inline code`  
\*\*strong\*\* - rendered as **strong**  
\_\_strong\_\_ - rendered as __strong__  
\*emphasis\* - rendered as *emphasis*  
\_emphasis\_ - rendered as _emphasis_  

#### Block Modifiers

##### Code Blocks

To create a code block indent every line by 4 or more spaces, or by a tab.

	int x = 0; // example code block
	x = x + 1;

##### Paragraphs

Paragraphs are consecutive lines of text
separated by one or more blank lines.  End a line with two or
more spaces to create a line break. 

##### Headings

\# This is an H1  
\#\# This is an H2  
\#\# This is also an H2 \#\#    
\#\#\#\#\# This is an H5  

##### Lists

Unordered lists use `*`, `+`, or `-` as bullets.

*   one
*   two

Numbered lists use numbers followed by periods:

1.  one
2.  two

##### Block Quotes

> Block quotes use email-style quoting  
> second quoted line

##### Horizontal Rule

A horizontal rule is created with 3 or more hyphens or stars on a line.

\-\-\-  
\*\*\*

#### Links

&lt;http://www.example.com&gt;  - rendered as <http://www.example.com>  
\[Text\]\(http://www.example.com "Optional title"\) - rendered as [Text](http://www.example.com "Optional title")  
\[an example\]\[id\] - rendered as [an example][id]

##### Link Reference Definition

[id]: http://example.com/  \"Optional Title Here\"

#### Images

\!\[Alt text\]\(path/to/image.png "Optional title"\)  
\!\[Alt text\]\[Reference ID\]

#### HTML Tags

All HTML tags are recognized
 
&lt;tt&gt;teletype&lt;/tt&gt; - <tt>teletype</tt>

#### Reference 

[Complete Markdown syntax guide](http://daringfireball.net/projects/markdown/syntax)

about.html,介绍

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>About</title>
<meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
</head>
<body lang="EN-US">
<h2>About This Content</h2>
 
<p>June 25, 2008</p>	
<h3>License</h3>

<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;).  Unless otherwise 
indicated below, the Content is provided to you under the terms and conditions of the
Eclipse Public License Version 1.0 (&quot;EPL&quot;).  A copy of the EPL is available 
at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
For purposes of the EPL, &quot;Program&quot; will mean the Content.</p>

<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is 
being redistributed by another party (&quot;Redistributor&quot;) and different terms and conditions may
apply to your use of any object code in the Content.  Check the Redistributor's license that was 
provided with the Content.  If no such license exists, contact the Redistributor.  Unless otherwise
indicated below, the terms and conditions of the EPL still apply to any source code in the Content
and such source code may be obtained at <a href="/">http://www.eclipse.org</a>.</p>

</body>
</html>

plugin.properties,属性配置

###############################################################################
# Copyright (c) 2012, 2013 Stefan Seelmann and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#     Stefan Seelmann - initial API and implementation
###############################################################################

Bundle-Vendor.0 = Eclipse Mylyn
Bundle-Name.0 = Mylyn WikiText Markdown UI

content-type.name = Markdown WikiText Markup

template.description.br = line break
template.description.h1 = Heading 1
template.description.h2 = Heading 2
template.description.h3 = Heading 3
template.description.h4 = Heading 4
template.description.h5 = Heading 5
template.description.h6 = Heading 6
template.description.lb = List (bulleted)
template.description.ln = List (numeric)
template.description.bq = Block quote
template.description.li = Link (inline) - [Text](http://www.example.com "Optional title")
template.description.lr = Link (footnote) - [Text][id]
template.description.ls = Link (simple) - <http://www.example.com>
template.description.ii = Image (inline) - ![Alt text](path/to/image.png "Optional title")
template.description.ir = Image (footnote) - ![Alt text][id]

plugin.xml,配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
   <extension point="org.eclipse.mylyn.wikitext.ui.markupLanguage">
      <language class="org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage"
            name="Markdown" fileExtensions="markdown,md,mdtext">
      </language>
   </extension>

   <extension point="org.eclipse.core.contenttype.contentTypes">
      <content-type id="org.eclipse.mylyn.wikitext.markdown" base-type="org.eclipse.mylyn.wikitext"
            file-extensions="markdown,md,mdtext" name="%content-type.name" priority="normal">
      </content-type>
   </extension>

   <extension point="org.eclipse.team.core.fileTypes">
      <fileTypes extension="markdown" type="text"/>
   </extension>

   <extension point="org.eclipse.mylyn.wikitext.ui.markupValidationRule">
      <rule markupLanguage="Markdown"
            class="org.eclipse.mylyn.wikitext.markdown.internal.validation.MarkdownReferenceValidationRule">
      </rule>
      <rule markupLanguage="Markdown"
            class="org.eclipse.mylyn.wikitext.markdown.internal.validation.LinkDefinitionValidationRule">
      </rule>
   </extension>
   <extension point="org.eclipse.mylyn.wikitext.ui.contentAssist">
      <templates markupLanguage="Markdown">

         <template name="# " description="%template.description.h1" content="\n# ${text}\n\n" block="true"/>
         <template name="## " description="%template.description.h2" content="\n## ${text}\n\n" block="true"/>
         <template name="### " description="%template.description.h3" content="\n### ${text}\n\n" block="true"/>
         <template name="#### " description="%template.description.h4" content="\n#### ${text}\n\n" block="true"/>
         <template name="##### " description="%template.description.h5" content="\n##### ${text}\n\n" block="true"/>
         <template name="###### " description="%template.description.h6" content="\n###### ${text}\n\n" block="true"/>

         <template name="*" description="%template.description.lb" content="\n*   ${first_item}\n*   ${second_item}\n" block="true"/>
         <template name="1." description="%template.description.ln" content="\n1.  ${first_item}\n2.  ${second_item}\n" block="true"/>
         <template name="&gt;" description="%template.description.bq" content="\n&gt; ${text}\n" block="true"/>

		<template name="[]" description="%template.description.li" content="[${text}](${url} &quot;${title}&quot;) $"/>
		<template name="[id]" description="%template.description.lr" content="[${text}][${id}] ${cursor}\n\n[${id}]: ${url} &quot;${title}&quot;"/>
		<template name="&lt;&gt;" description="%template.description.ls" content="&lt;http://${url}&gt; $"/>
		<template name="![]" description="%template.description.ii" content="![${altText}](${image}.png &quot;${title}&quot;) $"/>
		<template name="![id]" description="%template.description.ir" content="![${altText}][${id}] ${cursor}\n\n[${id}]: ${image}.png &quot;${title}&quot;"/>
		<template name="\n" description="Line break (end of line)" content="^  \n"/>
		<template name="\t" description="Code block (indented with tab)" content="\n\t${text}\n"/>

		<template name="`inline code`" description="A span of code inline" content="`${text}` $"/>
		<template name="**strong**" description="strong" content="**${text}** $"/>
		<template name="_emphasis_" description="emphasis" content="_${text}_ $"/>


		<template name="(c)" description="Copyright symbol" content="&amp;copy; $"/>
		<template name="(tm)" description="Trademark symbol" content="&amp;trade; $"/>
		<template name="&lt;hr/&gt;" description="Horizontal rule" content="\n------------------------------------------------------------\n\n"/>
      </templates>
   </extension>
   <extension
         point="org.eclipse.mylyn.wikitext.ui.cheatSheet">
      <content
            markupLanguage="Markdown"
            contentLanguage="Markdown"
            resource="help/cheatSheet/Markdown.md">
      </content>
   </extension>
</plugin>

通过这个就可以发现,支持的语法数量不多。标签template是支持的语法配置(标题、有序列表、无序列表、嵌套列表、水平线、斜体、加粗、内联代码、段落代码、引用、链接、图片等)。不支持表格、删除线,这个通过测试,也能发现。

支持的后缀为markdown,md,mdtext的文档。

markdown核心源码包,就简单放出一些吧,这是初始化操作,装载解析器:

package org.eclipse.mylyn.wikitext.markdown;

import org.eclipse.mylyn.wikitext.parser.markup.token.*;
import org.eclipse.mylyn.wikitext.parser.markup.phrase.*;
import org.eclipse.mylyn.wikitext.markdown.internal.token.*;
import org.eclipse.mylyn.wikitext.parser.*;
import org.eclipse.mylyn.wikitext.markdown.internal.phrase.*;
import java.util.*;
import org.eclipse.mylyn.wikitext.markdown.internal.block.*;
import org.eclipse.mylyn.wikitext.markdown.internal.util.*;
import java.io.*;
import org.eclipse.mylyn.wikitext.parser.markup.*;
import org.eclipse.mylyn.wikitext.markdown.internal.*;

public class MarkdownLanguage extends AbstractMarkupLanguage
{
    public MarkdownLanguage() {
        this.setName("Markdown");
    }
    
    protected void addStandardTokens(final AbstractMarkupLanguage.PatternBasedSyntax tokenSyntax) {
        tokenSyntax.add((PatternBasedElement)new PreserverHtmlEntityToken());
        tokenSyntax.add((PatternBasedElement)new InlineLinkReplacementToken());
        tokenSyntax.add((PatternBasedElement)new InlineImageReplacementToken());
        tokenSyntax.add((PatternBasedElement)new PatternLineBreakReplacementToken("( {2,})$"));
    }
    
    protected void addStandardPhraseModifiers(final AbstractMarkupLanguage.PatternBasedSyntax phraseModifierSyntax) {
        phraseModifierSyntax.add((PatternBasedElement)new HtmlEndTagPhraseModifier());
        phraseModifierSyntax.add((PatternBasedElement)new HtmlStartTagPhraseModifier());
        phraseModifierSyntax.add((PatternBasedElement)new InlineImageReplacementToken());
        phraseModifierSyntax.add((PatternBasedElement)new ReferenceStyleImageReplacementToken());
        phraseModifierSyntax.add((PatternBasedElement)new InlineLinkReplacementToken());
        phraseModifierSyntax.add((PatternBasedElement)new ReferenceStyleLinkReplacementToken());
        phraseModifierSyntax.add((PatternBasedElement)new AutomaticLinkReplacementToken());
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("**"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("__"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("*"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("_"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("\\"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("`"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("{"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("}"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("["));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("]"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("("));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier(")"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("#"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("+"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("-"));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("."));
        phraseModifierSyntax.add((PatternBasedElement)new BackslashEscapePhraseModifier("!"));
        phraseModifierSyntax.add((PatternBasedElement)new SimplePhraseModifier("``", DocumentBuilder.SpanType.CODE));
        phraseModifierSyntax.add((PatternBasedElement)new SimplePhraseModifier("`", DocumentBuilder.SpanType.CODE));
        phraseModifierSyntax.add((PatternBasedElement)new SimplePhraseModifier("**", DocumentBuilder.SpanType.STRONG));
        phraseModifierSyntax.add((PatternBasedElement)new SimplePhraseModifier("__", DocumentBuilder.SpanType.STRONG));
        phraseModifierSyntax.add((PatternBasedElement)new SimplePhraseModifier("*", DocumentBuilder.SpanType.EMPHASIS));
        phraseModifierSyntax.add((PatternBasedElement)new SimplePhraseModifier("_", DocumentBuilder.SpanType.EMPHASIS));
    }
    
    protected void addStandardBlocks(final List<Block> blocks, final List<Block> paragraphBreakingBlocks) {
        final CodeBlock codeBlock = new CodeBlock();
        final HorizontalRuleBlock horizontalRuleBlock = new HorizontalRuleBlock();
        final HeadingBlock headingBlock = new HeadingBlock();
        final InlineHtmlBlock inlineHtmlBlock = new InlineHtmlBlock();
        final QuoteBlock quoteBlock = new QuoteBlock();
        final ListBlock listBlock = new ListBlock();
        final LinkDefinitionBlock linkDefinitionBlock = new LinkDefinitionBlock();
        blocks.add(codeBlock);
        blocks.add(horizontalRuleBlock);
        blocks.add(headingBlock);
        blocks.add(inlineHtmlBlock);
        blocks.add(quoteBlock);
        blocks.add(listBlock);
        blocks.add(linkDefinitionBlock);
        paragraphBreakingBlocks.add(horizontalRuleBlock);
        paragraphBreakingBlocks.add(headingBlock);
        paragraphBreakingBlocks.add(quoteBlock);
        paragraphBreakingBlocks.add(listBlock);
    }
    
    protected Block createParagraphBlock() {
        final ParagraphBlock paragraphBlock = new ParagraphBlock();
        final UnderlinedHeadingBlock headingBlock = new UnderlinedHeadingBlock();
        final ReadAheadDispatcher readAheadBlock = new ReadAheadDispatcher(new NestableBlock[] { headingBlock, paragraphBlock });
        return readAheadBlock;
    }
    
    protected ContentState createState() {
        return new MarkdownContentState();
    }
    
    public DocumentBuilder createDocumentBuilder(final Writer out, final boolean formatting) {
        return (DocumentBuilder)new MarkdownDocumentBuilder(out);
    }
    
    public IdGenerationStrategy getIdGenerationStrategy() {
        return new GfmIdGenerationStrategy();
    }
}

wikitext.ui包中,找到菜单的配置信息,如果有汉化需求可以改这里,plugin.properties:

###############################################################################
# Copyright (c) 2007, 2013 David Green and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#     David Green - initial API and implementation
###############################################################################

content-type.name.0 = WikiText

menu.label.0 = WikiText
menu.tooltip.0 = Commands for WikiText lightweight markup
command.label.0 = Generate Docbook
command.tooltip.0 = Generate Docbook from WikiText source
command.label.1 = Generate HTML
command.tooltip.1 = Generate HTML from WikiText source
command.label.2 = Generate Eclipse Help
command.tooltip.2 = Generate Eclipse Help (*.html and *-toc.xml) from WikiText source
command.name.0 = Generate Docbook
command.name.1 = Generate HTML
command.name.2 = Generate Eclipse Help (*.html and *-toc.xml)
command.name.3 = Show Markup Cheat Sheet
command.label.3 = Markup Cheat Sheet
command.tooltip.3 = show help for wiki markup syntax
editor.name.0 = WikiText Editor
context.description.0 = WikiText markup editing context
context.name.0 = WikiText Markup Source Context
context.description.1 = WikiText markup editing context
context.name.1 = WikiText Markup Source Context
category.description.0 = commands for editing lightweight markup
category.name.0 = WikiText Markup Editing Commands
command.description.0 = display the markup cheat sheet
command.name.4 = Display Cheat Sheet
page.name.0 = WikiText
Bundle-Vendor.0 = Eclipse Mylyn
Bundle-Name.0 = Mylyn WikiText UI
page.name.1 = WikiText
page.name.2 = Templates

folding.label=Folding
empty.label

command.description.5 = Open a popup dialog with a quick outline of the current document
command.name.5 = Quick Outline

contextType.0=Lightweight Markup

themeElementCategory.label=WikiText
fontDefinition.text.label=Text Font
fontDefinition.text.description=Text font used when displaying text in the WikiText editor.
fontDefinition.monospace.label=Monospace Font
fontDefinition.monospace.description=Text font used when displaying monospace text in the WikiText editor.\n\
	Used to display text that is teletype, preformatted or code.
	
command.name = Generate Markup
commandParameter.name = TargetLanguage
extension.name = WikiText Nature
extension.name.0 = WikiText Validation
extension-point.name = Markup Language Cheat Seet
extension-point.name.0 = Markup Language Content Assist
relativeFilePathHyperlinkDetector-extension-point.name = File reference regex hyperlink detector extension point

最后,在放出自己写的测试文档:

标题  

主标题文字(最高阶标题)
==
副标题文字(第二阶标题)
--
# 大纲标题文字 H1(等同 主标题)
## 大纲标题文字 H2(等同 副标题)
### 大纲标题文字 H3
#### 大纲标题文字 H4
##### 大纲标题文字 H5
###### 大纲标题文字 H6
####### 大纲标题文字 H7(最多6阶)

_斜体_ 或  *斜体*

__粗体__ 或 **粗体**

普通段落


用空行分隔,连着多个空行也是一个效果,只能空一行的间距。
有的语法要用到空行才有效果(如主标题,列表),不空行的话默认又是会跟随上面语法特征(如表格语法前面就要空行后才有效果),应用时多加注意

水平分割线,3个以上符号,三个星号*为标准  
**
***
-----
___

强制换行  
在行尾输入  两个空格和换行

[链接文字--](http://www.baidu.com?链接地址 "你猜呢")
  
[百度][1]

前后id要匹配一致,一般为数字,这样可以在文档的其他任意地方快速插入这个链接。

[1]: http://www.baidu.com "拜拜啦"

链接<http://www.baidu.com>

引入图片  
![图片](https://www.csdn.net/app/img/wxfix.jpg)

有序列表:直接数字加英文句点加空格(最多三个空格)和文字  
 1. 这是1
 3. 这是1
    1. 嵌套1,三个空格
        1. 嵌套1,4个空格
            1. 嵌套1,4个空格
                1. 嵌套1,4个空格
 
 无序列表:文字前面加 星号空格*(空格)或 短横空格-(空格)或 加号+(空格)表示
 * 哦*
 - 嗯-
 + 哈+
    * 嵌套*,3个空格
       - 嵌套-,3个空格
          + 嵌套+,3个空格
 
在文字左右使用反引号包含,` 行内(内联)代码 `就可以了  

    段落文本代码块
       每行缩进4个空格或 1个 Tab表示


``` 注释
代码块
```

引用
> 第一行引用文字  
>> 第二行引用文字
>>> w
dfdadf

空一行就结束了

[name]: http://www.baidu.com "名称"
[home]: http://www.baidu.com "首页"
[支持]: http://www.baidu.com "瞎写的"

这里是[名][name],这里是[首][home],这里是[瞎][支持].

效果图:

就这样吧,先记录到这里,回头有时间在研究。如果有什么不足之处,望指教,小弟先谢过了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值