Skip to content

Commit c211e5e

Browse files
authored
Merge pull request #2052 from daspecster/fix-docs-source-line-links
Remove sys.path's from source link.
2 parents c805391 + 3f02dfe commit c211e5e

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

scripts/generate_json_docs.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@
2222
import pdoc
2323
from parinx.parser import parse_docstring
2424
from parinx.errors import MethodParsingException
25+
import six
2526

2627
from verify_included_modules import get_public_modules
2728

28-
import gcloud
29-
30-
ABSOLUTE_LIBRARY_PATH = os.path.dirname(os.path.dirname(os.path.abspath(
31-
gcloud.__file__)))
32-
3329

3430
class Module(object):
3531

@@ -46,17 +42,14 @@ def __init__(self, module_id, name, description=None,
4642
def from_module_name(cls, name, base_path):
4743
module = pdoc.Module(pdoc.import_module(name), allsubmodules=True)
4844
methods = module.functions() + module.variables()
49-
50-
mod = __import__(name)
51-
5245
examples = []
5346

5447
if '__init__' in name:
5548
snippets = get_snippet_examples(name.split('.')[1],
5649
os.path.join(base_path, 'docs'))
5750
examples.extend(snippets)
5851

59-
source_path = clean_source_path(inspect.getsourcefile(mod))
52+
source_path = clean_source_path(module)
6053

6154
return cls(module_id=name,
6255
name=name.split('.')[-1].title(),
@@ -90,8 +83,7 @@ def from_class_name(cls, module, kls):
9083
methods = kls.methods()
9184

9285
examples = []
93-
source_module = __import__(module.name)
94-
source_path = clean_source_path(inspect.getsourcefile(source_module))
86+
source_path = clean_source_path(module)
9587

9688
return cls(module_id=kls.name,
9789
name=kls.name.split('.')[-1].title(),
@@ -254,19 +246,19 @@ def build_link_from_type(type_name, object_type=None):
254246
type_markup = '<a data-custom-type="%s"' % doc_path
255247
if object_type == 'instance':
256248
type_markup += ' data-method="%s"' % type_name
257-
type_markup += '>%s</a>' % type_name
249+
type_markup += '>%s</a>' % (type_name,)
258250

259251
return type_markup
260252

261253

262-
def build_source(mod, method):
263-
if isinstance(mod, (types.ModuleType, types.ClassType,
264-
types.MethodType, types.FunctionType,
265-
types.TracebackType, types.FrameType,
266-
types.CodeType, types.TypeType)):
254+
def build_source(module, method):
255+
if isinstance(module, (types.ModuleType, types.ClassType,
256+
types.MethodType, types.FunctionType,
257+
types.TracebackType, types.FrameType,
258+
types.CodeType, types.TypeType)):
267259

268-
line = inspect.getsourcelines(mod)[1]
269-
source_path = clean_source_path(inspect.getsourcefile(mod))
260+
_, line = inspect.getsourcelines(module)
261+
source_path = clean_source_path(module)
270262

271263
if line:
272264
source_path = source_path + '#L' + str(line)
@@ -294,12 +286,14 @@ def build_type(type_id, title, contents):
294286
}
295287

296288

297-
def clean_source_path(source):
298-
source_path = ''
299-
if ABSOLUTE_LIBRARY_PATH in source:
300-
source_path = source.replace(ABSOLUTE_LIBRARY_PATH, source)
301-
302-
return source_path
289+
def clean_source_path(module):
290+
if isinstance(module, six.string_types):
291+
source_id = module
292+
elif hasattr(module, 'refname'):
293+
source_id = module.refname
294+
else:
295+
source_id = inspect.getmodule(module).__name__
296+
return '%s.py' % (source_id.replace('.', '/'),)
303297

304298

305299
def process_code_blocks(doc):
@@ -321,7 +315,7 @@ def process_code_blocks(doc):
321315
is_code = True
322316

323317
if is_code:
324-
block = {'code': '<pre><code>%s</code></pre>' % block}
318+
block = {'code': '<pre><code>%s</code></pre>' % (block,)}
325319

326320
formatted_blocks.append(block)
327321
return formatted_blocks
@@ -349,11 +343,11 @@ def process_words(line):
349343

350344
if word.startswith('``') and word.endswith('``'):
351345
word = word.replace('``', '')
352-
word = '<code>%s</code>' % word
346+
word = '<code>%s</code>' % (word,)
353347

354348
if word.startswith('**') and word.endswith('**'):
355349
word = word.replace('**', '')
356-
word = '<b>%s</b>' % word
350+
word = '<b>%s</b>' % (word,)
357351

358352
if word.startswith(':class:'):
359353
word = word.replace(':class:', '').replace('`', '')
@@ -379,7 +373,7 @@ def process_words(line):
379373
if end_sentence:
380374
word += '.'
381375

382-
processed_line += ' %s' % word
376+
processed_line += ' %s' % (word,)
383377

384378
processed_line = processed_line.replace('::', '')
385379

0 commit comments

Comments
 (0)