def __init__(self, doc_url, gallery_dir, relative=False): self.doc_url = doc_url self.gallery_dir = gallery_dir self.relative = relative self._link_cache = {} if doc_url.startswith(('http://', 'https://')): if relative: raise ValueError('Relative links are only supported for local ' 'URLs (doc_url cannot be absolute)') index_url = doc_url + '/' searchindex_url = doc_url + '/searchindex.js' else: index_url = os.path.join(doc_url, 'index.html') searchindex_url = os.path.join(doc_url, 'searchindex.js') # detect if we are using relative links on a Windows system if (os.name.lower() == 'nt' and not doc_url.startswith( ('http://', 'https://'))): if not relative: raise ValueError('You have to use relative=True for the local' ' package on a Windows system.') self._is_windows = True else: self._is_windows = False # Download and find documentation options. index = get_data(index_url, gallery_dir) self._docopts = parse_sphinx_docopts(index) # download and initialize the search index sindex = get_data(searchindex_url, gallery_dir) self._searchindex = js_index.loads(sindex)
def __init__(self, doc_url, gallery_dir, relative=False): self.doc_url = doc_url self.gallery_dir = gallery_dir self.relative = relative self._link_cache = {} if doc_url.startswith(('http://', 'https://')): if relative: raise ExtensionError( 'Relative links are only supported for local ' 'URLs (doc_url cannot be absolute)') index_url = doc_url + '/' searchindex_url = doc_url + '/searchindex.js' docopts_url = doc_url + '_static/documentation_options.js' else: index_url = os.path.join(doc_url, 'index.html') searchindex_url = os.path.join(doc_url, 'searchindex.js') docopts_url = os.path.join(doc_url, '_static', 'documentation_options.js') # detect if we are using relative links on a Windows system if (os.name.lower() == 'nt' and not doc_url.startswith( ('http://', 'https://'))): if not relative: raise ExtensionError( 'You have to use relative=True for the local' ' package on a Windows system.') self._is_windows = True else: self._is_windows = False # Download and find documentation options. As of Sphinx 1.7, these # options are now kept in a standalone file called # 'documentation_options.js'. Since SphinxDocLinkResolver can be called # not only for the documentation which is being built but also ones # that are being referenced, we need to try and get the index page # first and if that doesn't work, check for the # documentation_options.js file. index = get_data(index_url, gallery_dir) if 'var DOCUMENTATION_OPTIONS' in index: self._docopts = parse_sphinx_docopts(index) else: docopts = get_data(docopts_url, gallery_dir) self._docopts = parse_sphinx_docopts(docopts) # download and initialize the search index sindex = get_data(searchindex_url, gallery_dir) self._searchindex = js_index.loads(sindex)
def __init__(self, doc_url, gallery_dir, relative=False): self.doc_url = doc_url self.gallery_dir = gallery_dir self.relative = relative self._link_cache = {} if doc_url.startswith(('http://', 'https://')): if relative: raise ValueError('Relative links are only supported for local ' 'URLs (doc_url cannot be absolute)') index_url = doc_url + '/' searchindex_url = doc_url + '/searchindex.js' docopts_url = doc_url + '_static/documentation_options.js' else: index_url = os.path.join(doc_url, 'index.html') searchindex_url = os.path.join(doc_url, 'searchindex.js') docopts_url = os.path.join(doc_url, '_static', 'documentation_options.js') # detect if we are using relative links on a Windows system if (os.name.lower() == 'nt' and not doc_url.startswith(('http://', 'https://'))): if not relative: raise ValueError('You have to use relative=True for the local' ' package on a Windows system.') self._is_windows = True else: self._is_windows = False # Download and find documentation options. As of Sphinx 1.7, these # options are now kept in a standalone file called # 'documentation_options.js'. Since SphinxDocLinkResolver can be called # not only for the documentation which is being built but also ones that # are being referenced, we need to try and get the index page first and # if that doesn't work, check for the documentation_options.js file. index = get_data(index_url, gallery_dir) if 'var DOCUMENTATION_OPTIONS' in index: self._docopts = parse_sphinx_docopts(index) else: docopts = get_data(docopts_url, gallery_dir) self._docopts = parse_sphinx_docopts(docopts) # download and initialize the search index sindex = get_data(searchindex_url, gallery_dir) self._searchindex = js_index.loads(sindex)
def merge(path, include_terms=False, exclude_old=True): """Merge searchindex files in subdirectories of `path` and create a searchindex files under `path` """ groups = {} merger = SearchIndexMerger() for entry in os.listdir(path): index_path = os.path.join(path, entry, "searchindex.js") if not os.path.exists(index_path): continue namespace, version = os.path.basename( os.path.dirname(index_path)).split("-") with open(index_path, "rb") as h: data = h.read() mod = js_index.loads(data) groups.setdefault(namespace, []).append((version, mod)) def sort_version_key(version): return tuple(map(int, version.split("."))) for namespace, entries in sorted(groups.items()): # newest first, for name clashes entries.sort(key=lambda x: sort_version_key(x[0]), reverse=True) if exclude_old: entries for i, (version, mod) in enumerate(entries): key = namespace + "-" + version if exclude_old and i > 0: merger.add_index(key, None) continue fixup_props_signals(mod) if not include_terms: mod["terms"].clear() mod["titleterms"].clear() merger.add_index(key, mod) with open(os.path.join(path, "searchindex.js"), "wb") as h: output = merger.merge() h.write(js_index.dumps(output))
def load_index(self, namespace, index_path): with open(index_path, "rb") as h: data = h.read() mod = js_index.loads(data) self.add_index(namespace, mod)
def load_index(self, namespace, index_path): with io.open(index_path, "r", encoding="utf-8") as h: data = h.read() mod = js_index.loads(data) self.add_index(namespace, mod)