Browse Source

[docs] added python docs to generate python docs

version-14
Rushabh Mehta 12 years ago
parent
commit
2ad2b37f19
5 changed files with 104 additions and 23 deletions
  1. +6
    -0
      public/js/wn/model/model.js
  2. +1
    -0
      public/js/wn/ui/appframe.js
  3. +69
    -21
      public/js/wn/views/docs.js
  4. +3
    -2
      webnotes/__init__.py
  5. +25
    -0
      webnotes/utils/docs.py

+ 6
- 0
public/js/wn/model/model.js View File

@@ -202,6 +202,12 @@ $.extend(wn.model, {
/* help: Attach a trigger on change of a particular field.
To trigger on any change in a particular doctype, use fieldname as "*"
*/
/* example: wn.model.on("Customer", "age", function(fieldname, value, doc) {
if(doc.age < 16) {
msgprint("Warning, Customer must atleast be 16 years old.");
raise "CustomerAgeError";
}
}) */
wn.provide("wn.model.events." + doctype + "." + fieldname);
wn.model.events[doctype][fieldname] = fn;
},


+ 1
- 0
public/js/wn/ui/appframe.js View File

@@ -49,6 +49,7 @@ wn.ui.AppFrame = Class.extend({
},
set_title: function(txt, full_text) {
this.title = txt;
document.title = txt;
this.$w.find(".breadcrumb .appframe-title").html(txt);
this.$w.find(".title-text").html(txt);
},


+ 69
- 21
public/js/wn/views/docs.js View File

@@ -1,5 +1,13 @@
/*

Todo:
- add DocTypes
- separate pages for classes
- decorage python objects
- make tocs
- write docs
- export html files (with clean links)

Documentation API

Every module (namespace) / class will have a page
@@ -88,10 +96,25 @@ wn.docs.DocsPage = Class.extend({
init: function(opts) {
/* docs: create js documentation */
$.extend(this, opts);
this.make();
var obj = wn.provide(this.namespace),
me = this;

if($.isEmptyObject(obj)) {
wn.call({
"method": "webnotes.utils.docs.get_docs",
args: {
"module_name": this.namespace
},
callback: function(r) {
me.make(r.message);
}
})
} else {
this.make(obj);
}
},
make: function() {
var obj = wn.provide(this.namespace);
make: function(obj) {
$("<h2>").html(obj._label || this.namespace).appendTo(this.parent);
this.make_breadcrumbs(obj);
this.make_intro(obj);
@@ -138,7 +161,7 @@ wn.docs.DocsPage = Class.extend({
var me = this,
classes = {}
$.each(obj, function(name, value) {
if(value._type=="class") {
if(value && value._type=="class") {
classes[name] = value;
};
})
@@ -161,8 +184,8 @@ wn.docs.DocsPage = Class.extend({
get_functions: function(obj) {
var functions = {};
$.each(obj, function(name, value) {
if(typeof value=="function"
&& (value._type || "function")=="function")
if(value && ((typeof value==="function" && typeof value.init !== "function")
|| value._type === "function"))
functions[name] = value;
});
return functions;
@@ -187,23 +210,18 @@ wn.docs.DocsPage = Class.extend({
namespace = namespace + ".";
if(code.indexOf("/* options")===-1) {
var args = code.split("function")[1].split("(")[1].split(")")[0];
var args = this.get_args(value);
options = this.make_parameters("Parameters",
JSON.parse(this.get_property(value, "parameters") || "{}"));
} else {
var args = "options",
options = JSON.parse(code.split("/* options:")[1].split("*/")[0]);

options = "<h5>Options: </h5><table class='table table-bordered'><tbody>"
+ $.map(options, function(o, i) {
var i = o.indexOf(":");
return repl('<tr>\
<td style="width: 30%">%(key)s</td>\
<td>%(value)s</td></tr>', {
key: o.slice(0, i),
value: o.slice(i+1)
})
}).join("") + "</tbody></table>";
options = this.make_parameters("Options",
JSON.parse(this.get_property(value, "options") || "{}"));
}
var example = this.get_property(value, "example");
example = example ? ("<h5>Example</h5><pre>" + example.replace(/\t/g, "") + "</pre>") : "";
var help = code.split("/* help:")[1]
if(help) help = help.split("*/")[0];
$(repl('<tr>\
@@ -212,15 +230,45 @@ wn.docs.DocsPage = Class.extend({
%(help)s\
<h5>Usage:</h5>\
<pre>%(namespace)s%(name)s(%(args)s)</pre>\
%(options)s\
%(options)s%(example)s\
</td>\
</tr>', {
name: name,
namespace: namespace,
args: args,
example: example || "",
options: options || "",
help: help ? ("<p>" + help + "</p>") : ""
})).appendTo(parent)
},
get_args: function(obj) {
if(obj._args)
return obj._args.join(", ");
else
return obj.toString().split("function")[1].split("(")[1].split(")")[0];
},
get_property: function(obj, property) {
if(obj["_" + property])
return obj["_" + property];
var code = obj.toString();
if(code.indexOf("/* " + property + ":")!==-1) {
return code.split("/* " + property + ":")[1].split("*/")[0]
}
return "";
},
make_parameters: function(title, options) {
if($.isEmptyObject(options))
return "";
return "<h5>"+title+"</h5><table class='table table-bordered'><tbody>"
+ $.map(options, function(o, i) {
var i = o.indexOf(":");
return repl('<tr>\
<td style="width: 30%">%(key)s</td>\
<td>%(value)s</td></tr>', {
key: o.slice(0, i),
value: o.slice(i+1)
})
}).join("") + "</tbody></table>";
}
})

+ 3
- 2
webnotes/__init__.py View File

@@ -19,13 +19,13 @@
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

from __future__ import unicode_literals
"""
globals attached to webnotes module
+ some utility functions that should probably be moved
"""

from __future__ import unicode_literals

code_fields_dict = {
'Page':[('script', 'js'), ('content', 'html'), ('style', 'css'), ('static_content', 'html'), ('server_code', 'py')],
'DocType':[('server_code_core', 'py'), ('client_script_core', 'js')],
@@ -314,6 +314,7 @@ def doclist(lst=None):
return DocList(lst)

def bean(doctype=None, name=None, copy=None):
"""return an instance of the object, wrapped as a Bean (webnotes.model.bean)"""
from webnotes.model.bean import Bean
if copy:
return Bean(copy_doclist(copy))


+ 25
- 0
webnotes/utils/docs.py View File

@@ -0,0 +1,25 @@
"""Documentation Generation"""

import webnotes

@webnotes.whitelist()
def get_docs(module_name):
import inspect, importlib
docs = {}
module = importlib.import_module(module_name)
docs["_intro"] = getattr(module, "__doc__", "")
for name in dir(module):
value = getattr(module, name)
if inspect.isfunction(value):
docs[name] = {
"_type": "function",
"_args": inspect.getargspec(value)[0],
"_help": getattr(value, "__doc__", "")
}
return docs
if __name__=="__main__":
print get_docs("webnotes")

Loading…
Cancel
Save