Commit ca52b4b3 authored by Andy Stewart's avatar Andy Stewart
Browse files

Add mermaid plugin.

parent 852e32ad
Loading
Loading
Loading
Loading
+20 −17
Original line number Diff line number Diff line
@@ -47,14 +47,14 @@ EAF is an extensible framework, one can develop any Qt5 application and integrat
1. Make sure to have ```python3``` installed, and use ```pip3``` to install all EAF dependencies (see below list for details)

```Bash
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser pyinotify markdown
```

    If you use Arch Linux, we recommend you install dependencies with below command:

```Bash
sudo pacman -S python-pyqt5 python-pyqt5-sip python-pyqtwebengine python-xlib python-qrcode python-feedparser
python-dbus
python-dbus python-pyinotify python-markdown
yay -S python-pymupdf python-grip
```

@@ -118,6 +118,8 @@ Packages listed as **Core** are mandatory for EAF to work, whereas other package
| aria2         | pacman (Arch) | Browser                                                                            | Download files from the web                   |
| wetty         | yarn          | Terminal                                                                           | Communicate between browser and local TTY     |
| libreoffice   | pacman        | Doc Viewer                                                                         | Convert doc file to pdf                       |
| pyinotify     | pacman        | Mermaid                                                                            | Monitor *.mmd file change status              |
| markdown      | pacman        | Mermaid                                                                            | Covert markdown format to mermaid html format |

## Launch EAF Applications
| Application Name    | Launch                                                                 |
@@ -138,6 +140,7 @@ Packages listed as **Core** are mandatory for EAF to work, whereas other package
| RSS Reader          | `M-x eaf-open-rss-reader`                                              |
| Mindmap             | `M-x eaf-create-mindmap` or `M-x eaf-open-mindmap`                     |
| Doc Viewer          | `M-x eaf-open-office`                                                  |
| Mermaid             | `M-x eaf-open` Mermaid file (*.mmd)                                    |
| Demo                | `M-x eaf-open-demo` to verify basic functionality                      |

- To open the file under the cursor in `dired` using appropriate EAF Application, use `eaf-open-this-from-dired` instead.
@@ -194,7 +197,7 @@ If you got "undefined symbol" error after start EAF, and you use Arch Linux, yes
You need use pip install all dependences after you upgrade your Arch system, then undefine symbol error will fix.

```Bash
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser --force-reinstall
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser pyinotify markdown --force-reinstall
```

### What is Github Personal Access Tokens?
+11 −7
Original line number Diff line number Diff line
@@ -45,15 +45,16 @@ EAF是一个可编程扩展的框架,你可以开发自己的Qt5应用并集
1. 先确认系统中已经安装了 ```python3```,然后使用 ```pip3``` 安装EAF依赖库:

```Bash
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser aria2p
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf
grip qrcode feedparser pyinotify markdown
```

    如果你使用的是Arch系统,建议用下面的命令来安装依赖:

```Bash
sudo pacman -S python-pyqt5 python-pyqt5-sip python-pyqtwebengine python-xlib python-qrcode python-feedparser
python-dbus
yay -S python-pymupdf python-grip aria2p
python-dbus python-pyinotify python-markdown
yay -S python-pymupdf python-grip
```

    因为Arch的QtWebEngine编译的时候内建私有编解码库,可以直接在浏览器中播放mp4等视频文件。
@@ -113,10 +114,11 @@ echo 'export LANG=zh_CN.UTF-8' >> ~/.bashrc
| grip          | pip3          | Markdown预览                                                         | 建立Markdown文件的HTML服务                         |
| qrcode        | pip3          | 文件上传,文件下载,文字传输                                         | 根据文件信息生成二维码                             |
| feedparser    | pip3          | RSS阅读器                                                            | 解析RSS/Atom信息                                   |
| aria2p        | pip3          | 浏览器                                                               | 发送下载请求给Aria2 Daemon                         |
| aria2         | pacman (Arch) | 浏览器                                                               | 下载网络文件                                       |
| wetty         | yarn          | 终端模拟器                                                           | 通过浏览器与本地TTY交互                            |
| libreoffice   | pacman        | 办公文档阅读器                                                       | 转换doc文件为pdf格式                               |
| pyinotify     | pacman        | 流程图                                                               | 监听 mmd 格式文件的变动                            |
| markdown      | pacman        | 流程图                                                               | 转换 mmd 格式为 mermaid 识别的 html 格式           |

## EAF应用启动命令
| 应用名称       | 启动命令                                                                    |
@@ -137,6 +139,7 @@ echo 'export LANG=zh_CN.UTF-8' >> ~/.bashrc
| RSS新闻阅读器  | `M-x eaf-open-rss-reader`                                                   |
| 思维导图       | `M-x eaf-create-mindmap` or `M-x eaf-open-mindmap`                          |
| 办公文档阅读器 | `M-x eaf-open-office`                                                       |
| 流程图         | `M-x eaf-open` 输入 mmd 格式文件                                            |
| 演示程序       | `M-x eaf-open-demo`                                                         |

-`dired`文件管理器中,建议绑定按键到命令 `eaf-open-this-from-dired` ,它会自动用合适的EAF应用来打开文件。
@@ -189,7 +192,8 @@ EAF依赖X11的XReparent技术,Wayland并不支持跨进程窗口融合技术
如果你启动的时候发现 "undefined symbol" 错误,并且你使用的是Arch系统,那肯定是因为Arch自身的bug,Arch系统每次升级以后,重新使用pip3安装一次Python依赖包就可以解决这个问题:

```Bash
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf grip qrcode feedparser aria2p --force-reinstall
sudo pip3 install dbus-python python-xlib pyqt5 pyqtwebengine pymupdf
grip qrcode feedparser pyinotify markdown --force-reinstall
```

### Github 个人访问标记干什么用的?

app/mermaid/buffer.py

0 → 100644
+64 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (C) 2018 Andy Stewart
#
# Author:     Andy Stewart <lazycat.manatee@gmail.com>
# Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from PyQt5 import QtCore
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QColor
from core.browser import BrowserBuffer
from core.utils import string_to_base64
from core.utils import PostGui
import threading
import pyinotify
import markdown
import os

class AppBuffer(BrowserBuffer):

    update_content = QtCore.pyqtSignal()

    def __init__(self, buffer_id, url, config_dir, arguments, emacs_var_dict):
        BrowserBuffer.__init__(self, buffer_id, url, config_dir, arguments, emacs_var_dict, False, QColor(255, 255, 255, 255))

        self.url = url
        self.render()

        self.update_content.connect(self.render)
        threading.Timer(1, self.monitor_file_change).start()

    def monitor_file_change(self):
        parent = self

        class ModHandler(pyinotify.ProcessEvent):
            def process_IN_CLOSE_WRITE(self, evt):
                parent.update_content.emit()

        handler = ModHandler()
        wm = pyinotify.WatchManager()
        notifier = pyinotify.Notifier(wm, handler)
        wdd = wm.add_watch(self.url, pyinotify.IN_CLOSE_WRITE)
        notifier.loop()

    def render(self):
        with open(self.url, "r") as f:
            html = markdown.markdown(f.read(), extensions=['app.mermaid.md_mermaid'])
            self.buffer_widget.setHtml(html)
+83 −0
Original line number Diff line number Diff line
"""
Mermaid Extension for Python-Markdown
========================================

Adds mermaid parser (like github-markdown) to standard Python-Markdown code blocks.

Original code Copyright 2018-2020 [Olivier Ruelle].

License: GNU GPLv3

"""

from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor

import re
import os

MermaidRegex = re.compile(r"^(?P<mermaid_sign>[\~\`]){3}[\ \t]*[Mm]ermaid[\ \t]*$")


# ------------------ The Markdown Extension -------------------------------

class MermaidPreprocessor(Preprocessor):
    def run(self, lines):
        new_lines = []
        mermaid_sign = ""
        m_start = None
        m_end = None
        in_mermaid_code = False
        is_mermaid = False
        old_line = ""

        js_file = "file://" + (os.path.join(os.path.dirname(__file__), "node_modules", "mermaid", "dist", "mermaid.js"))

        new_lines.append('<meta charset="utf-8">')
        new_lines.append('<script src="{}"></script>'.format(js_file))
        new_lines.append('<script>mermaid.initialize({startOnLoad:true});</script>')

        for line in lines:
            # Wait for starting line with MermaidRegex (~~~ or ``` following by [mM]ermaid )
            if not in_mermaid_code:
                m_start = MermaidRegex.match(line)
            else:
                m_end = re.match(r"^["+mermaid_sign+"]{3}[\ \t]*$", line)
                if m_end:
                    in_mermaid_code = False

            if m_start:
                in_mermaid_code = True
                mermaid_sign = m_start.group("mermaid_sign")
                if not re.match(r"^[\ \t]*$", old_line):
                    new_lines.append("")
                if not is_mermaid:
                    is_mermaid = True
                new_lines.append('<div class="mermaid">')
                m_start = None
            elif m_end:
                new_lines.append('</div>')
                new_lines.append("")
                m_end = None
            elif in_mermaid_code:
                new_lines.append(line.strip())
            else:
                new_lines.append(line)

            old_line = line

        return new_lines


class MermaidExtension(Extension):
    """ Add source code hilighting to markdown codeblocks. """

    def extendMarkdown(self, md, md_globals):
        """ Add HilitePostprocessor to Markdown instance. """
        # Insert a preprocessor before ReferencePreprocessor
        md.preprocessors.register(MermaidPreprocessor(md), 'mermaid', 35)

        md.registerExtension(self)

def makeExtension(**kwargs):  # pragma: no cover
    return MermaidExtension(**kwargs)
+1 −0
Original line number Diff line number Diff line
../css-b64-images/bin/css-b64-images
 No newline at end of file
Loading