エロサイトの作り方

2013年11月から勉強しながらエロサイトを作っています。

Sublime Text 3でHTML-CSS-JS Prettifyを入れる

インストール

Package Controllから

  • Cmd+Shift+Pinstallと入力
  • htmlprettifyと入力

使う

ファイルを開いてCmd+Shift+H

Node.js was not found in the default path. Please specify the location.

しかしエラーが出る。

エラーの解消

OKを押すと、HTMLPrettify.sublime-settingsを開いてくれる。

初期の状態

{
  // Simply using `node` without specifying a path sometimes doesn't work :(
  // https://github.com/victorporof/Sublime-HTMLPrettify#oh-noez-command-not-found
  // http://nodejs.org/#download
  "node_path": {
    "windows": "C:/Program Files/nodejs/node.exe",
    "linux": "/usr/bin/nodejs",
    "osx": "/usr/local/bin/node"
  },
  "format_on_save": false,
  "format_selection_only": true,
  "print_diagnostics": true
}

nodeの場所を確認

$ /usr/local/bin/node -v
zsh: no such file or directory: /usr/local/bin/node

ここには無いですね。

$ which node
/Users/hentai-kun/.nvm/v0.10.28/bin/node

このパスに変えれば動くはず。

この問題をググる"node_path": "path-to-node"形式で書かれているページが出てきますが、古いバージョン向けなのか、今のバージョンでは動かないです。

(補足参照)

正しい設定

{
  // Simply using `node` without specifying a path sometimes doesn't work :(
  // https://github.com/victorporof/Sublime-HTMLPrettify#oh-noez-command-not-found
  // http://nodejs.org/#download
  "node_path": {
    "windows": "C:/Program Files/nodejs/node.exe",
    "linux": "/usr/bin/nodejs",
    "osx": "/Users/hentai-kun/.nvm/v0.10.28/bin/node"
  },
  "format_on_save": false,
  "format_selection_only": true,
  "print_diagnostics": true
}

OSX環境なので、"osx"のところを書き換えます。

再び使う

ファイルを開いてCmd+Shift+H

今度はちゃんと動きました。

補足:ソース上の記載

HTMLPretty.py

  @staticmethod
  def exists_in_path(cmd):
    # Can't search the path if a directory is specified.
    assert not os.path.dirname(cmd)
    path = os.environ.get("PATH", "").split(os.pathsep)
    extensions = os.environ.get("PATHEXT", "").split(os.pathsep)

    # For each directory in PATH, check if it contains the specified binary.
    for directory in path:
      base = os.path.join(directory, cmd)
      options = [base] + [(base + ext) for ext in extensions]
      for filename in options:
        if os.path.exists(filename):
          return True

    return False

  @staticmethod
  def get_node_path():
    # Simply using `node` without specifying a path sometimes doesn't work :(
    if PluginUtils.exists_in_path("nodejs"):
      return "nodejs"
    elif PluginUtils.exists_in_path("node"):
      return "node"
    else:
      platform = sublime.platform()
      node = PluginUtils.get_pref("node_path").get(platform)
      print("Using node.js path on '" + platform + "': " + node)
      return node

やっていることとしては、

  • 環境変数のPATHかPATHEXT(Windowsのみ)にnodejsが含まれていれば、ディレクトリ無しのnodejsで呼び出せると期待する
  • 環境変数のPATHかPATHEXT(Windowsのみ)にnodeが含まれていれば、ディレクトリ無しのnodeで呼び出せると期待する
  • どちらも無ければ、設定ファイルのnode_path['platform']の設定値を使う

という動作のようです。

設定を変えても動かないという記事が幾つかありましたが、環境変数に設定されていて、その設定が間違っている場合には動かなそうですね。