Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

# -*- coding: utf-8 -* 

r""" 

Process docstrings with Sphinx 

 

Processes docstrings with Sphinx. Can also be used as a commandline script: 

 

``python sphinxify.py <text>`` 

 

AUTHORS: 

 

- Tim Joseph Dumol (2009-09-29): initial version 

""" 

 

#***************************************************************************** 

# Copyright (C) 2009 Tim Dumol <tim@timdumol.com> 

# 

# Distributed under the terms of the BSD License 

#***************************************************************************** 

from __future__ import absolute_import, print_function 

 

import os 

import re 

import shutil 

from tempfile import mkdtemp 

from sage.env import SAGE_DOC_SRC 

from sphinx.application import Sphinx 

 

 

def sphinxify(docstring, format='html'): 

r""" 

Runs Sphinx on a ``docstring``, and outputs the processed 

documentation. 

 

INPUT: 

 

- ``docstring`` -- string -- a ReST-formatted docstring 

 

- ``format`` -- string (optional, default 'html') -- either 'html' or 

'text' 

 

OUTPUT: 

 

- string -- Sphinx-processed documentation, in either HTML or 

plain text format, depending on the value of ``format`` 

 

EXAMPLES:: 

 

sage: from sage.misc.sphinxify import sphinxify 

sage: sphinxify('A test') 

'...<div class="docstring">\n \n <p>A test</p>\n\n\n</div>' 

sage: sphinxify('**Testing**\n`monospace`') 

'...<div class="docstring"...<strong>Testing</strong>\n<span class="math"...</p>\n\n\n</div>' 

sage: sphinxify('`x=y`') 

'...<div class="docstring">\n \n <p><span class="math">x=y</span></p>\n\n\n</div>' 

sage: sphinxify('`x=y`', format='text') 

'x=y\n' 

sage: sphinxify(':math:`x=y`', format='text') 

'x=y\n' 

 

TESTS:: 

 

sage: n = len(sys.path) 

sage: _ = sphinxify('A test') 

sage: assert n == len(sys.path) 

""" 

srcdir = mkdtemp() 

base_name = os.path.join(srcdir, 'docstring') 

rst_name = base_name + '.rst' 

 

if format == 'html': 

suffix = '.html' 

else: 

suffix = '.txt' 

output_name = base_name + suffix 

 

with open(rst_name, 'w') as filed: 

filed.write(docstring) 

 

# Sphinx constructor: Sphinx(srcdir, confdir, outdir, doctreedir, 

# buildername, confoverrides, status, warning, freshenv). 

confdir = os.path.join(SAGE_DOC_SRC, 'en', 'introspect') 

 

open(os.path.join(srcdir, 'docutils.conf'), 'w').write(r""" 

[parsers] 

smart_quotes = no 

""") 

doctreedir = os.path.join(srcdir, 'doctrees') 

confoverrides = {'html_context': {}, 'master_doc': 'docstring'} 

 

import sys 

old_sys_path = list(sys.path) # Sphinx modifies sys.path 

sphinx_app = Sphinx(srcdir, confdir, srcdir, doctreedir, format, 

confoverrides, None, None, True) 

sphinx_app.build(None, [rst_name]) 

sys.path = old_sys_path 

 

# We need to remove "_" from __builtin__ that the gettext module installs 

from six.moves import builtins 

builtins.__dict__.pop('_', None) 

 

if os.path.exists(output_name): 

output = open(output_name, 'r').read() 

output = output.replace('<pre>', '<pre class="literal-block">') 

 

# Translate URLs for media from something like 

# "../../media/...path.../blah.png" 

# or 

# "/media/...path.../blah.png" 

# to 

# "/doc/static/reference/media/...path.../blah.png" 

output = re.sub("""src=['"](/?\.\.)*/?media/([^"']*)['"]""", 

'src="/doc/static/reference/media/\\2"', 

output) 

# Remove spurious \(, \), \[, \]. 

output = output.replace('\\(', '').replace('\\)', '').replace('\\[', '').replace('\\]', '') 

else: 

print("BUG -- Sphinx error") 

if format == 'html': 

output = '<pre class="introspection">%s</pre>' % docstring 

else: 

output = docstring 

 

shutil.rmtree(srcdir, ignore_errors=True) 

 

return output 

 

 

if __name__ == '__main__': 

import sys 

if len(sys.argv) == 2: 

print(sphinxify(sys.argv[1])) 

else: 

print("""Usage: 

%s 'docstring' 

 

docstring -- docstring to be processed 

""")