2019年9月29日日曜日

rstファイルをvimで編集リアルタイム更新

rstファイルをリアルタイムでプレビューできるriv.vimをインストールしました。ちょっと大変でした。Python2と3が混ざっている感があります。

Ubuntu16.04 LTS ではOK。
Windowsでは×です。

インストール方法(無理やり感あり)


riv.vim (vim) とInstantRst (web server) を組み合わせます。サーバはflaskと同じ仕組みを使っているようです。

riv.vim, InstanRstを入れる。instant-rst.pyを入れる。こちらはPython2.7用しかないので、追加の設定が要りました。

・python2.7のディレクトリのパーミッションを追加する。

pipでThe directory '/home/.../.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

と出るので sudo -H でインストールしました。

・python2.7のPATHを追加する。


 instantRst test.rst で起動すると、こんなエラーが出ていました。
 instantRst -f test.rst
test.rst
WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
 * Serving Flask app "instant_rst.server" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off

Some error/exception occurred.
(, TypeError('port must be an integer',), )


Portがintになっていないということで見てみると、Python2から3に値を渡すときに変換されているということでした。

エラーの該当部分はpython2.7/dist-packages/werkzeug/serving.py で、追加した結果はこうなりました。

port = int(native(port))  

if not isinstance(port, int):
    raise TypeError("port must be an integer")

入力されるportをtype(port)として調べると、型は future.types.newint.newint となっており、これは

from future.utils import native
int(native(port))

でintにできました。nattive(port) によって long にしてからint を使っています。

https://python-future.org/what_else.html

これで無事にInstantRstサーバが立ち上がって、ほぼリアルタムでrstファイルの更新ができます。

使い方メモ

起動するには、vim で rstファイルを開いた状態で
:InstantRst
これでブラウザが立ち上がります。

デフォルトのブラウザはfirefoxですが、
let instant_rst_browser='usr/bin/google-chrome'
とすることでクロムに変えました。vimrcにこれを書いても効果ないようです。

0 件のコメント:

コメントを投稿