Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3831)

Unified Diff: chrome/common/extensions/docs/server2/reference_resolver.py

Issue 11315018: Extensions Docs Server: Generalize $ref's to work for any schema node (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/reference_resolver.py
diff --git a/chrome/common/extensions/docs/server2/reference_resolver.py b/chrome/common/extensions/docs/server2/reference_resolver.py
new file mode 100644
index 0000000000000000000000000000000000000000..10b6010a94f8b8303a0a9a71849526d7704f38c4
--- /dev/null
+++ b/chrome/common/extensions/docs/server2/reference_resolver.py
@@ -0,0 +1,65 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from file_system import FileNotFoundError
+
+def _ClassifySchemaNode(node_name, api):
+ if '.' in node_name:
+ node_name, rest = node_name.split('.', 1)
+ else:
+ rest = None
+ for key, group in [('types', 'type'),
+ ('functions', 'method'),
+ ('events', 'event'),
+ ('properties', 'property')]:
+ for item in api.get(key, []):
+ if item['name'] == node_name:
+ if rest is not None:
+ ret = _ClassifySchemaNode(rest, item)
+ if ret is not None:
+ return ret
+ else:
+ return group
+ return None
+
+class ReferenceResolver(object):
+ """Resolves references to $ref's by searching through the APIs to find the
+ correct node.
+ """
+ def __init__(self, api_data_source, api_list_data_source):
+ self._api_data_source = api_data_source
+ self._api_list_data_source = api_list_data_source
+
+ def GetLinkToRefType(self, namespace_name, ref_type):
not at google - send to devlin 2012/11/02 17:26:48 The "type" seems incorrect now that we're resolvin
cduvall 2012/11/03 01:30:05 Done.
+ if ref_type.startswith('%s.' % namespace_name):
+ ref_type = ref_type[len('%s.' % namespace_name):]
+ if '.' not in ref_type:
+ try:
+ category = _ClassifySchemaNode(
+ ref_type,
+ self._api_data_source[namespace_name])
not at google - send to devlin 2012/11/02 17:26:48 General comment: do we need all these __getitem__
cduvall 2012/11/03 01:30:05 Took out the __getitem__ stuff.
+ except FileNotFoundError:
+ return None
+ if category is None:
+ return None
+ return { 'href': '#%s-%s' % (category, ref_type), 'text': ref_type }
+ parts = ref_type.split('.')
+ api_list = self._api_list_data_source.GetAllNames()
+ for i, part in enumerate(parts):
+ if '.'.join(parts[:i]) not in api_list:
+ continue
+ try:
+ api_name = '.'.join(parts[:i])
+ api = self._api_data_source[api_name]
+ except FileNotFoundError:
+ continue
+ name = '.'.join(parts[i:])
+ category = _ClassifySchemaNode(name, api)
+ if category is None:
+ continue
+ return {
+ 'href': '%s.html#%s-%s' % (api_name, category, name.replace('.', '-')),
+ 'text': ref_type
+ }
+ return None

Powered by Google App Engine
This is Rietveld 408576698