doc/tools/pyjsrest.py
branchstable
changeset 6880 4be32427b2b9
parent 5470 fb004819cab4
child 7233 614f23606091
equal deleted inserted replaced
6879:54944faf9b61 6880:4be32427b2b9
     2 """
     2 """
     3 Parser for Javascript comments.
     3 Parser for Javascript comments.
     4 """
     4 """
     5 from __future__ import with_statement
     5 from __future__ import with_statement
     6 
     6 
       
     7 import os.path as osp
     7 import sys, os, getopt, re
     8 import sys, os, getopt, re
     8 
     9 
     9 def clean_comment(match):
    10 def clean_comment(match):
    10     comment = match.group()
    11     comment = match.group()
    11     comment = strip_stars(comment)
    12     comment = strip_stars(comment)
    69 
    70 
    70     rst_dir = opts.get('--output') or opts.get('-o')
    71     rst_dir = opts.get('--output') or opts.get('-o')
    71     if rst_dir is None and len(args) != 1:
    72     if rst_dir is None and len(args) != 1:
    72         rst_dir = 'apidocs'
    73         rst_dir = 'apidocs'
    73     js_dir = opts.get('--jspath') or opts.get('-p')
    74     js_dir = opts.get('--jspath') or opts.get('-p')
    74     if not os.path.exists(os.path.join(rst_dir)):
    75     if not osp.exists(osp.join(rst_dir)):
    75         os.makedirs(os.path.join(rst_dir))
    76         os.makedirs(osp.join(rst_dir))
    76 
    77 
    77     f_index = open(os.path.join(rst_dir, 'index.rst'), 'wb')
    78     index = set()
    78     f_index.write('''
    79     for js_path, js_dirs, js_files in os.walk(js_dir):
       
    80         rst_path = re.sub('%s%s*' % (js_dir, osp.sep), '', js_path)
       
    81         for js_file in js_files:
       
    82             if not js_file.endswith('.js'):
       
    83                 continue
       
    84             if js_file in FILES_TO_IGNORE:
       
    85                 continue
       
    86             if not osp.exists(osp.join(rst_dir, rst_path)):
       
    87                 os.makedirs(osp.join(rst_dir, rst_path))
       
    88             rst_content =  extract_rest(js_path, js_file)
       
    89             filename = osp.join(rst_path, js_file[:-3])
       
    90             # add to index
       
    91             index.add(filename)
       
    92             # save rst file
       
    93             with open(osp.join(rst_dir, filename) + '.rst', 'wb') as f_rst:
       
    94                 f_rst.write(rst_content)
       
    95     stream = open(osp.join(rst_dir, 'index.rst'), 'w')
       
    96     stream.write('''
    79 .. toctree::
    97 .. toctree::
    80     :maxdepth: 1
    98     :maxdepth: 1
    81 
    99 
    82 '''
   100 ''')
    83 )
   101     # first write expected files in order
    84     for js_path, js_dirs, js_files in os.walk(js_dir):
   102     for fileid in INDEX_IN_ORDER:
    85         rst_path = re.sub('%s%s*' % (js_dir, os.path.sep), '', js_path)
   103         try:
    86         for js_file in js_files:
   104             index.remove(fileid)
    87             if not js_file.endswith('.js'):
   105         except:
    88                 continue
   106             raise Exception(
    89             if not os.path.exists(os.path.join(rst_dir, rst_path)):
   107         'Bad file id %s referenced in INDEX_IN_ORDER in %s, '
    90                 os.makedirs(os.path.join(rst_dir, rst_path))
   108         'fix this please' % (fileid, __file__))
    91             rst_content =  extract_rest(js_path, js_file)
   109         stream.write('    %s\n' % fileid)
    92             filename = os.path.join(rst_path, js_file[:-3])
   110     # append remaining, by alphabetical order
    93             # add to index
   111     for fileid in sorted(index):
    94             f_index.write('    %s\n' % filename)
   112         stream.write('    %s\n' % fileid)
    95             # save rst file
   113     stream.close()
    96             with open(os.path.join(rst_dir, filename) + '.rst', 'wb') as f_rst:
       
    97                 f_rst.write(rst_content)
       
    98     f_index.close()
       
    99 
   114 
   100 def extract_rest(js_dir, js_file):
   115 def extract_rest(js_dir, js_file):
   101     js_filepath = os.path.join(js_dir, js_file)
   116     js_filepath = osp.join(js_dir, js_file)
   102     filecontent = open(js_filepath, 'U').read()
   117     filecontent = open(js_filepath, 'U').read()
   103     comments = get_doc_comments(filecontent)
   118     comments = get_doc_comments(filecontent)
   104     rst = rest_title(js_file, 0)
   119     rst = rest_title(js_file, 0)
   105     rst += '.. module:: %s\n\n' % js_file
   120     rst += '.. module:: %s\n\n' % js_file
   106     rst += '\n\n'.join(comments)
   121     rst += '\n\n'.join(comments)
   107     return rst
   122     return rst
   108 
   123 
       
   124 INDEX_IN_ORDER = [
       
   125     'cubicweb',
       
   126     'cubicweb.python',
       
   127     'cubicweb.htmlhelpers',
       
   128     'cubicweb.ajax',
       
   129 
       
   130     'cubicweb.lazy',
       
   131     'cubicweb.tabs',
       
   132     'cubicweb.ajax.box',
       
   133     'cubicweb.facets',
       
   134     'cubicweb.widgets',
       
   135     'cubicweb.image',
       
   136     'cubicweb.flot',
       
   137     'cubicweb.calendar',
       
   138     'cubicweb.preferences',
       
   139     'cubicweb.edition',
       
   140     'cubicweb.reledit',
       
   141     'cubicweb.iprogress',
       
   142     'cubicweb.rhythm',
       
   143     'cubicweb.gmap',
       
   144     'cubicweb.timeline-ext',
       
   145 ]
       
   146 
       
   147 FILES_TO_IGNORE = set([
       
   148     'jquery.js',
       
   149     'jquery.treeview.js',
       
   150     'jquery.json.js',
       
   151     'jquery.tablesorter.js',
       
   152     'jquery.timePicker.js',
       
   153     'jquery.flot.js',
       
   154     'jquery.corner.js',
       
   155     'jquery.ui.js',
       
   156     'ui.core.js',
       
   157     'ui.tabs.js',
       
   158     'ui.slider.js',
       
   159     'excanvas.js',
       
   160     'gmap.utility.labeledmarker.js',
       
   161 
       
   162     'cubicweb.fckcwconfig.js',
       
   163     'cubicweb.fckcwconfig-full.js',
       
   164     'cubicweb.goa.js',
       
   165     'cubicweb.compat.js',
       
   166     'cubicweb.timeline-bundle.js',
       
   167     ])
       
   168 
   109 if __name__ == '__main__':
   169 if __name__ == '__main__':
   110     parse_js_files()
   170     parse_js_files()