diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2013-12-19 00:46:36 -0200 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2014-01-10 16:40:38 -0200 |
commit | 74a46a38dbe37eb4a8bca9af9f65a3289797bfbe (patch) | |
tree | 293a0f37e91f8565094e3530061c341e6fa4dea2 | |
parent | 7c3c9c8794ddeb4a61afa44817b6e267be771e2c (diff) | |
download | luarocks-74a46a38dbe37eb4a8bca9af9f65a3289797bfbe.tar.gz luarocks-74a46a38dbe37eb4a8bca9af9f65a3289797bfbe.tar.bz2 luarocks-74a46a38dbe37eb4a8bca9af9f65a3289797bfbe.zip |
First go on `luarocks doc`
-rw-r--r-- | src/luarocks/doc.lua | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/luarocks/doc.lua b/src/luarocks/doc.lua new file mode 100644 index 00000000..9fb9d688 --- /dev/null +++ b/src/luarocks/doc.lua | |||
@@ -0,0 +1,146 @@ | |||
1 | |||
2 | --- Module implementing the LuaRocks "doc" command. | ||
3 | -- Shows documentation for an installed rock. | ||
4 | module("luarocks.doc", package.seeall) | ||
5 | |||
6 | local util = require("luarocks.util") | ||
7 | local show = require("luarocks.show") | ||
8 | local path = require("luarocks.path") | ||
9 | local dir = require("luarocks.dir") | ||
10 | local fetch = require("luarocks.fetch") | ||
11 | local fs = require("luarocks.fs") | ||
12 | --[[ | ||
13 | local search = require("luarocks.search") | ||
14 | local cfg = require("luarocks.cfg") | ||
15 | local deps = require("luarocks.deps") | ||
16 | local manif = require("luarocks.manif") | ||
17 | ]] | ||
18 | help_summary = "Shows documentation for an installed rock." | ||
19 | |||
20 | help = [[ | ||
21 | <argument> is an existing package name. | ||
22 | Without any flags, tries to load the documentation | ||
23 | using a series of heuristics. | ||
24 | With these flags, return only the desired information: | ||
25 | |||
26 | --home Open the home page of project. | ||
27 | |||
28 | For more information about a rock, see the 'show' command. | ||
29 | ]] | ||
30 | |||
31 | --[[ | ||
32 | local function keys_as_string(t, sep) | ||
33 | return table.concat(util.keys(t), sep or " ") | ||
34 | end | ||
35 | |||
36 | local function word_wrap(line) | ||
37 | local width = tonumber(os.getenv("COLUMNS")) or 80 | ||
38 | if width > 80 then width = 80 end | ||
39 | if #line > width then | ||
40 | local brk = width | ||
41 | while brk > 0 and line:sub(brk, brk) ~= " " do | ||
42 | brk = brk - 1 | ||
43 | end | ||
44 | if brk > 0 then | ||
45 | return line:sub(1, brk-1) .. "\n" .. word_wrap(line:sub(brk+1)) | ||
46 | end | ||
47 | end | ||
48 | return line | ||
49 | end | ||
50 | |||
51 | local function format_text(text) | ||
52 | text = text:gsub("^%s*",""):gsub("%s$", ""):gsub("\n[ \t]+","\n"):gsub("([^\n])\n([^\n])","%1 %2") | ||
53 | local paragraphs = util.split_string(text, "\n\n") | ||
54 | for n, line in ipairs(paragraphs) do | ||
55 | paragraphs[n] = word_wrap(line) | ||
56 | end | ||
57 | return (table.concat(paragraphs, "\n\n"):gsub("%s$", "")) | ||
58 | end | ||
59 | |||
60 | local function module_name(mod, filename, name, version, repo, manifest) | ||
61 | local base_dir | ||
62 | if filename:match("%.lua$") then | ||
63 | base_dir = path.deploy_lua_dir(repo) | ||
64 | else | ||
65 | base_dir = path.deploy_lib_dir(repo) | ||
66 | end | ||
67 | |||
68 | return dir.path(base_dir, filename) | ||
69 | end | ||
70 | ]] | ||
71 | |||
72 | --- Driver function for "doc" command. | ||
73 | -- @param name or nil: an existing package name. | ||
74 | -- @param version string or nil: a version may also be passed. | ||
75 | -- @return boolean: True if succeeded, nil on errors. | ||
76 | function run(...) | ||
77 | local flags, name, version = util.parse_flags(...) | ||
78 | if not name then | ||
79 | return nil, "Argument missing. "..util.see_help("doc") | ||
80 | end | ||
81 | |||
82 | local repo | ||
83 | name, version, repo = show.pick_installed_rock(name, version, flags["tree"]) | ||
84 | if not name then | ||
85 | return nil, version | ||
86 | end | ||
87 | |||
88 | local rockspec, err = fetch.load_local_rockspec(path.rockspec_file(name, version, repo)) | ||
89 | if not rockspec then return nil,err end | ||
90 | local descript = rockspec.description or {} | ||
91 | |||
92 | if flags["home"] then | ||
93 | util.printout("Opening "..descript.homepage.." ...") | ||
94 | fs.browser(descript.homepage) | ||
95 | return true | ||
96 | end | ||
97 | |||
98 | local directory = path.install_dir(name,version,repo) | ||
99 | |||
100 | local docdir | ||
101 | local directories = { "doc", "docs" } | ||
102 | for _, d in ipairs(directories) do | ||
103 | local dirname = dir.path(directory, d) | ||
104 | if fs.is_dir(dirname) then | ||
105 | docdir = dirname | ||
106 | break | ||
107 | end | ||
108 | end | ||
109 | if not docdir then | ||
110 | if descript.homepage then | ||
111 | util.printout("Local documentation directory not found -- opening "..descript.homepage.." ...") | ||
112 | fs.browser(descript.homepage) | ||
113 | return true | ||
114 | end | ||
115 | return nil, "Documentation directory not found for "..name.." "..version | ||
116 | end | ||
117 | |||
118 | local files = fs.find(docdir) | ||
119 | local extensions = { ".html", ".md", ".txt", "" } | ||
120 | local basenames = { "index", "readme", "manual" } | ||
121 | |||
122 | for _, extension in ipairs(extensions) do | ||
123 | for _, basename in ipairs(basenames) do | ||
124 | local filename = basename..extension | ||
125 | local found | ||
126 | for _, file in ipairs(files) do | ||
127 | if file:lower():match(filename) and ((not found) or #file < #found) then | ||
128 | found = file | ||
129 | end | ||
130 | end | ||
131 | if found then | ||
132 | local pathname = dir.path(docdir, found) | ||
133 | util.printout("Opening "..pathname.." ...") | ||
134 | if not fs.browser(pathname) then | ||
135 | local fd = io.open(pathname, "r") | ||
136 | util.printout(fd:read("*a")) | ||
137 | fd:close() | ||
138 | end | ||
139 | return true | ||
140 | end | ||
141 | end | ||
142 | end | ||
143 | |||
144 | return true | ||
145 | end | ||
146 | |||