21
22
from spyderlib import dependencies
22
from spyderlib.baseconfig import get_conf_path, _
23
from spyderlib.baseconfig import get_conf_path, get_module_source_path, _
23
24
from spyderlib.ipythonconfig import IPYTHON_QT_INSTALLED
24
25
from spyderlib.config import CONF
25
26
from spyderlib.guiconfig import get_color_scheme, get_font, set_font
292
297
def __init__(self, html_text_no_doc=''):
293
298
super(SphinxThread, self).__init__()
294
301
self.html_text_no_doc = html_text_no_doc
296
302
self.math_option = False
298
def render(self, doc, math_option=False):
299
"""Start thread to render given doc string dictionary"""
304
def render(self, doc, context=None, math_option=False):
305
"""Start thread to render a given documentation"""
300
306
# If the thread is already running wait for it to finish before
301
307
# starting it again.
310
self.context = context
304
311
self.math_option = math_option
305
312
# This causes run() to be executed in separate thread
309
316
html_text = self.html_text_no_doc
311
if doc is not None and doc['docstring'] != '':
313
context = generate_context(name=doc['name'],
314
argspec=doc['argspec'],
316
math=self.math_option)
317
html_text = sphinxify(doc['docstring'], context)
318
except Exception as error:
319
self.emit(SIGNAL('error_msg(QString)'),
320
to_text_string(error))
319
if type(doc) is dict and 'docstring' in doc.keys() \
320
and doc['docstring'] != '':
322
context = generate_context(name=doc['name'],
323
argspec=doc['argspec'],
325
math=self.math_option)
326
html_text = sphinxify(doc['docstring'], context)
327
except Exception as error:
328
self.emit(SIGNAL('error_msg(QString)'),
329
to_text_string(error))
331
elif self.context is not None:
333
html_text = sphinxify(doc, self.context)
334
except Exception as error:
335
self.emit(SIGNAL('error_msg(QString)'),
336
to_text_string(error))
322
338
self.emit(SIGNAL('html_ready(QString)'), html_text)
601
623
self.switch_to_rich_text()
603
625
self.switch_to_plain_text()
605
def show_intro_message(self):
606
intro_message = _("Here you can get help of any object by pressing "
607
"%s in front of it, either on the Editor or the "
609
"Help can also be shown automatically after writing "
610
"a left parenthesis next to an object. You can "
611
"activate this behavior in %s.")
612
prefs = _("Preferences > Object Inspector")
613
if self.is_rich_text_mode():
615
intro_message = intro_message % ("<b>Ctrl+I</b>", "<br><br>",
617
self.set_rich_text_html(usage(title, intro_message),
618
QUrl.fromLocalFile(CSS_PATH))
620
install_sphinx = "\n\n%s" % _("Please consider installing Sphinx "
621
"to get documentation rendered in "
623
intro_message = intro_message % ("Ctrl+I", "\n\n", prefs)
624
intro_message += install_sphinx
625
self.set_plain_text(intro_message, is_code=False)
627
627
#------ Public API (related to rich/plain text widgets) --------------------
719
719
"""Set rich text"""
720
720
self.rich_text.set_html(html_text, base_url)
721
721
self.save_text([self.rich_text.set_html, html_text, base_url])
723
def show_intro_message(self):
724
intro_message = _("Here you can get help of any object by pressing "
725
"%s in front of it, either on the Editor or the "
727
"Help can also be shown automatically after writing "
728
"a left parenthesis next to an object. You can "
729
"activate this behavior in %s.")
730
prefs = _("Preferences > Object Inspector")
731
if self.is_rich_text_mode():
733
tutorial_message = _("New to Spyder? Read our")
734
tutorial = _("tutorial")
735
intro_message = intro_message % ("<b>Ctrl+I</b>", "<br><br>",
737
self.set_rich_text_html(usage(title, intro_message,
738
tutorial_message, tutorial),
739
QUrl.fromLocalFile(CSS_PATH))
741
install_sphinx = "\n\n%s" % _("Please consider installing Sphinx "
742
"to get documentation rendered in "
744
intro_message = intro_message % ("Ctrl+I", "\n\n", prefs)
745
intro_message += install_sphinx
746
self.set_plain_text(intro_message, is_code=False)
748
def show_rich_text(self, text, collapse=False, img_path=''):
749
"""Show text in rich mode"""
750
self.visibility_changed(True)
752
self.switch_to_rich_text()
753
context = generate_context(collapse=collapse, img_path=img_path)
754
self.render_sphinx_doc(text, context)
756
def show_plain_text(self, text):
757
"""Show text in plain mode"""
758
self.visibility_changed(True)
760
self.switch_to_plain_text()
761
self.set_plain_text(text, is_code=False)
763
def show_tutorial(self):
764
tutorial_path = get_module_source_path('spyderlib.utils.inspector')
765
img_path = osp.join(tutorial_path, 'static', 'images')
766
tutorial = osp.join(tutorial_path, 'tutorial.rst')
767
text = open(tutorial).read()
768
if sphinxify is not None:
769
self.show_rich_text(text, collapse=True, img_path=img_path)
771
self.show_plain_text(text)
773
def handle_link_clicks(self, url):
774
url = to_text_string(url.toString())
775
if url == "spy://tutorial":
777
elif url.startswith('http'):
778
programs.start_file(url)
780
self.rich_text.webview.load(QUrl(url))
723
782
#------ Public API ---------------------------------------------------------
724
783
def set_external_console(self, external_console):
876
935
self.shell = self.internal_shell
877
936
return self.shell
879
def render_sphinx_doc(self, doc):
938
def render_sphinx_doc(self, doc, context=None):
880
939
"""Transform doc string dictionary to HTML and show it"""
881
940
# Math rendering option could have changed
882
self._sphinx_thread.render(doc, self.get_option('math'))
941
self._sphinx_thread.render(doc, context, self.get_option('math'))
884
943
def _on_sphinx_thread_html_ready(self, html_text):
885
944
"""Set our sphinx documentation based on thread result"""