ページ「ログイン画面が真っ黒」と「MediaWikiの「構文ハイライトエラー」」の間の差分
Rin-scrooge (トーク | 投稿記録) |
Rin-scrooge (トーク | 投稿記録) |
||
1行目: | 1行目: | ||
− | + | さくらのレンタルサーバーで最新のMediaWikiを使用していることが前提なんだけど…<br/> | |
− | + | 「SyntaxHighlight」を使用すると、「構文ハイライトエラー」になってしまう…。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 原因は単純でさくらのレンタルサーバーのPythonは現時点で2.7…SyntaxHighlightはPython3が前提…<br/> | |
− | + | 当然うまく動かないわけです…。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | そこで、ホームディレクトリにPython3を入れて、そちらを参照するようにSyntaxHighlightを書き換えちゃいます。 | |
− | |||
− | |||
− | |||
− | |||
− | + | = Python3のインストール = | |
− | + | 参考サイトに基づいて、Pythonの最新版(Python3)をインストールしていきます。<br/> | |
+ | 先ずはホームディレクトリに作業用のディレクトリを作成して移動します。 | ||
+ | <syntaxhighlight lang="bash">mkdir ~/work | ||
+ | cd work</syntaxhighlight> | ||
+ | |||
+ | |||
+ | 次に、wgetコマンドでPythonの公式サイトからモジュールをダウンロードします。 | ||
+ | <syntaxhighlight lang="bash">wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz</syntaxhighlight> | ||
+ | (ダウンロードパスは適宜見直してください) | ||
+ | |||
+ | |||
+ | ダウンロードしたらtarコマンドで解凍します。 | ||
+ | <syntaxhighlight lang="bash">tar -xzf Python-3.7.4.tar.xz</syntaxhighlight> | ||
+ | |||
+ | |||
+ | 解凍すると「Python-3.7.4」ディレクトリが作成されますので、そこに移動します。 | ||
+ | <syntaxhighlight lang="bash">cd Python-3.7.4</syntaxhighlight> | ||
+ | |||
+ | |||
+ | 以下のコマンドでインストール先を変更します。 | ||
+ | <syntaxhighlight lang="bash">./configure --prefix=$HOME/local/python/</syntaxhighlight> | ||
+ | |||
+ | |||
+ | そしたらmakeしてインストールします。 | ||
+ | <syntaxhighlight lang="bash">make | ||
+ | make install</syntaxhighlight> | ||
+ | |||
+ | |||
+ | インストールに使用したディレクトリは削除しちゃいます。 | ||
+ | <syntaxhighlight lang="bash">cd ~ | ||
+ | rm -fr work</syntaxhighlight> | ||
+ | |||
+ | = SyntaxHighlightの編集 = | ||
+ | コードを直接編集するので、バックアップを取りながら進めていきます。 | ||
+ | |||
+ | |||
+ | 先ずは編集するコードが有るところまで移動します。<br/> | ||
+ | ターゲットは「create_pygmentize_bundle」ファイルです。 | ||
+ | <syntaxhighlight lang="bash">cd [MdiaWikiのディレクトリ]/extensions/SyntaxHighlight_GeSHi/pygments</syntaxhighlight> | ||
+ | [MdiaWikiのディレクトリ]は自分の環境に合わせて書き換えてください。 | ||
+ | |||
+ | |||
+ | バックアップを作成します。 | ||
+ | <syntaxhighlight lang="bash">cp create_pygmentize_bundle create_pygmentize_bundle_bk | ||
+ | mv pygmentize pygmentize_bk</syntaxhighlight> | ||
+ | (「pygmentize」ファイルは作成し直すので、mvしています) | ||
+ | |||
+ | |||
+ | 「create_pygmentize_bundle」ファイルを修正します。<br/> | ||
+ | 以下の様に修正してください | ||
+ | <syntaxhighlight lang="python" line highlight="1,19,60">#!/home/[userid]/local/python/bin/python3 | ||
+ | # -*- coding: utf-8 -*- | ||
+ | """ | ||
+ | Create a standalone, executable 'pygmentize' bundle. | ||
+ | Author: Ori Livneh | ||
+ | |||
+ | """ | ||
+ | import hashlib | ||
+ | import io | ||
+ | import os | ||
+ | import stat | ||
+ | import textwrap | ||
+ | import urllib.request | ||
+ | import xmlrpc.client | ||
+ | import zipfile | ||
+ | |||
+ | |||
+ | PYGMENTIZE_LAUNCHER = textwrap.dedent('''\ | ||
+ | #!/home/[userid]/local/python/bin/python3 | ||
+ | |||
+ | import sys | ||
+ | import pygments.cmdline | ||
+ | try: | ||
+ | sys.exit(pygments.cmdline.main(sys.argv)) | ||
+ | except KeyboardInterrupt: | ||
+ | sys.exit(1) | ||
+ | ''') | ||
+ | |||
+ | |||
+ | print('Querying PyPI for the latest Pygments release...') | ||
+ | pypi = xmlrpc.client.ServerProxy('https://pypi.python.org/pypi') | ||
+ | latest_version = pypi.package_releases('Pygments')[0] | ||
+ | url = None | ||
+ | for release in pypi.release_urls('Pygments', latest_version): | ||
+ | if (release['packagetype'] == 'bdist_wheel' and | ||
+ | release['python_version'].startswith('py2')): | ||
+ | url = release['url'] | ||
+ | md5_digest = release['md5_digest'] | ||
+ | break | ||
+ | |||
+ | if not url: | ||
+ | raise RuntimeError('No suitable package found.') | ||
+ | |||
+ | print('Retrieving version %s (%s)...' % (latest_version, url)) | ||
+ | req = urllib.request.urlopen(url) | ||
+ | buf = io.BytesIO(req.read()) | ||
+ | |||
+ | print('Verifying...') | ||
+ | if hashlib.md5(buf.getvalue()).hexdigest() != md5_digest: | ||
+ | raise RuntimeError('MD5 checksum mismatch.') | ||
+ | |||
+ | print('Creating executable ZIP bundle...') | ||
+ | with zipfile.ZipFile(buf, 'a') as zf: | ||
+ | zf.writestr('__main__.py', PYGMENTIZE_LAUNCHER) | ||
+ | |||
+ | data = buf.getvalue() | ||
+ | script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
+ | file_path = os.path.join(script_dir, 'pygmentize') | ||
+ | with open(file_path, 'wb') as f: | ||
+ | f.write(b'#!/home/[userid]/local/python/bin/python3\n') | ||
+ | f.write(data) | ||
+ | |||
+ | file_st = os.stat(file_path) | ||
+ | os.chmod(file_path, file_st.st_mode | stat.S_IEXEC) | ||
+ | |||
+ | with open(os.path.join(script_dir, 'VERSION'), 'w') as f: | ||
+ | f.write(latest_version + '\n') | ||
+ | |||
+ | print('Done. Wrote %s bytes to %s' % (len(data), file_path))</syntaxhighlight> | ||
+ | ([userid]は書き直してください) | ||
+ | |||
+ | |||
+ | 最後に「pygmentize」を作成し直します。 | ||
+ | <syntaxhighlight lang="bash">/home/[userid]/local/python/bin/python3 ./create_pygmentize_bundle</syntaxhighlight> | ||
+ | ([userid]は書き直してください) | ||
+ | |||
+ | = WEBブラウザのキャッシュ = | ||
+ | WEBブラウザのキャッシュが残っていると、なかなか変わった感じがしないかもしれません。<br/> | ||
+ | そんな時は例えば「<syntaxhighlight lang="bash">」を「<syntaxhighlight lang="bash" line>」などに書き直してみてください。<br/> | ||
+ | これで、行番号が表示されればOKです。 | ||
= 参考サイト = | = 参考サイト = | ||
− | [https:// | + | [https://emptypage.jp/notes/pymods-on-sakura.html さくらのレンタルサーバで Python 外部モジュールを使う]<br/> |
− | + | [https://affitips.com/pages/65.html さくらにPython3をインストールしたときのメモ - アフィリエイト&ウェブ制作のtips] | |
− | [https:// | ||
− | [[Category: | + | [[Category:さくらサーバー]] |
− | [[Category: | + | [[Category:MediaWiki]] |
+ | [[Category:Python3]] | ||
+ | [[Category:SyntaxHighlight]] |
2019年7月26日 (金) 05:01時点における版
さくらのレンタルサーバーで最新のMediaWikiを使用していることが前提なんだけど…
「SyntaxHighlight」を使用すると、「構文ハイライトエラー」になってしまう…。
原因は単純でさくらのレンタルサーバーのPythonは現時点で2.7…SyntaxHighlightはPython3が前提…
当然うまく動かないわけです…。
そこで、ホームディレクトリにPython3を入れて、そちらを参照するようにSyntaxHighlightを書き換えちゃいます。
Python3のインストール
参考サイトに基づいて、Pythonの最新版(Python3)をインストールしていきます。
先ずはホームディレクトリに作業用のディレクトリを作成して移動します。
mkdir ~/work
cd work
次に、wgetコマンドでPythonの公式サイトからモジュールをダウンロードします。
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
(ダウンロードパスは適宜見直してください)
ダウンロードしたらtarコマンドで解凍します。
tar -xzf Python-3.7.4.tar.xz
解凍すると「Python-3.7.4」ディレクトリが作成されますので、そこに移動します。
cd Python-3.7.4
以下のコマンドでインストール先を変更します。
./configure --prefix=$HOME/local/python/
そしたらmakeしてインストールします。
make
make install
インストールに使用したディレクトリは削除しちゃいます。
cd ~
rm -fr work
SyntaxHighlightの編集
コードを直接編集するので、バックアップを取りながら進めていきます。
先ずは編集するコードが有るところまで移動します。
ターゲットは「create_pygmentize_bundle」ファイルです。
cd [MdiaWikiのディレクトリ]/extensions/SyntaxHighlight_GeSHi/pygments
[MdiaWikiのディレクトリ]は自分の環境に合わせて書き換えてください。
バックアップを作成します。
cp create_pygmentize_bundle create_pygmentize_bundle_bk
mv pygmentize pygmentize_bk
(「pygmentize」ファイルは作成し直すので、mvしています)
「create_pygmentize_bundle」ファイルを修正します。
以下の様に修正してください
1 #!/home/[userid]/local/python/bin/python3
2 # -*- coding: utf-8 -*-
3 """
4 Create a standalone, executable 'pygmentize' bundle.
5 Author: Ori Livneh
6
7 """
8 import hashlib
9 import io
10 import os
11 import stat
12 import textwrap
13 import urllib.request
14 import xmlrpc.client
15 import zipfile
16
17
18 PYGMENTIZE_LAUNCHER = textwrap.dedent('''\
19 #!/home/[userid]/local/python/bin/python3
20
21 import sys
22 import pygments.cmdline
23 try:
24 sys.exit(pygments.cmdline.main(sys.argv))
25 except KeyboardInterrupt:
26 sys.exit(1)
27 ''')
28
29
30 print('Querying PyPI for the latest Pygments release...')
31 pypi = xmlrpc.client.ServerProxy('https://pypi.python.org/pypi')
32 latest_version = pypi.package_releases('Pygments')[0]
33 url = None
34 for release in pypi.release_urls('Pygments', latest_version):
35 if (release['packagetype'] == 'bdist_wheel' and
36 release['python_version'].startswith('py2')):
37 url = release['url']
38 md5_digest = release['md5_digest']
39 break
40
41 if not url:
42 raise RuntimeError('No suitable package found.')
43
44 print('Retrieving version %s (%s)...' % (latest_version, url))
45 req = urllib.request.urlopen(url)
46 buf = io.BytesIO(req.read())
47
48 print('Verifying...')
49 if hashlib.md5(buf.getvalue()).hexdigest() != md5_digest:
50 raise RuntimeError('MD5 checksum mismatch.')
51
52 print('Creating executable ZIP bundle...')
53 with zipfile.ZipFile(buf, 'a') as zf:
54 zf.writestr('__main__.py', PYGMENTIZE_LAUNCHER)
55
56 data = buf.getvalue()
57 script_dir = os.path.dirname(os.path.abspath(__file__))
58 file_path = os.path.join(script_dir, 'pygmentize')
59 with open(file_path, 'wb') as f:
60 f.write(b'#!/home/[userid]/local/python/bin/python3\n')
61 f.write(data)
62
63 file_st = os.stat(file_path)
64 os.chmod(file_path, file_st.st_mode | stat.S_IEXEC)
65
66 with open(os.path.join(script_dir, 'VERSION'), 'w') as f:
67 f.write(latest_version + '\n')
68
69 print('Done. Wrote %s bytes to %s' % (len(data), file_path))
([userid]は書き直してください)
最後に「pygmentize」を作成し直します。
/home/[userid]/local/python/bin/python3 ./create_pygmentize_bundle
([userid]は書き直してください)
WEBブラウザのキャッシュ
WEBブラウザのキャッシュが残っていると、なかなか変わった感じがしないかもしれません。
そんな時は例えば「<syntaxhighlight lang="bash">」を「<syntaxhighlight lang="bash" line>」などに書き直してみてください。
これで、行番号が表示されればOKです。
参考サイト
さくらのレンタルサーバで Python 外部モジュールを使う
さくらにPython3をインストールしたときのメモ - アフィリエイト&ウェブ制作のtips