Skip to content

Commit

Permalink
some of the rocky's changes in #202
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Feb 4, 2024
1 parent bcd523c commit 9572a48
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
26 changes: 11 additions & 15 deletions mathics_django/doc/django_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from mathics_django.settings import get_doctest_html_data_path

# FIXME: remove globalness
doctest_html_data_path = get_doctest_html_data_path(should_be_readable=True)
try:
doctest_html_data_path = get_doctest_html_data_path(should_be_readable=True)
with open(doctest_html_data_path, "rb") as doctest_html_data_file:
doc_data = pickle.load(doctest_html_data_file)
except IOError:
Expand All @@ -43,9 +43,9 @@
class DjangoDocElement:
def href(self, ajax=False):
if ajax:
return "javascript:loadDoc('%s')" % self.get_uri()
return f"javascript:loadDoc('{self.get_uri()}')"
else:
return "/doc%s" % self.get_uri()
return f"/doc{self.get_uri()}"

def get_prev(self):
return self.get_prev_next()[0]
Expand Down Expand Up @@ -212,7 +212,6 @@ def get_tests(self):
return tests

def html(self):
counters = {}
items = [item for item in self.items if not item.is_private()]
title_line = self.title + "\n"
if len(items) and items[0].text.startswith(title_line):
Expand All @@ -221,7 +220,7 @@ def html(self):
# Or that is the intent. This code is a bit hacky.
items[0].text = items[0].text[len(title_line) :]

text = "\n".join(item.html(counters) for item in items if not item.is_private())
text = "\n".join(item.html() for item in items if not item.is_private())
if text == "":
# HACK ALERT if text is "" we may have missed some test markup.
return mark_safe(escape_html(self.rawdoc))
Expand Down Expand Up @@ -262,7 +261,7 @@ def get_collection(self):
"""Return a list of parts in this doc"""
return self.doc.parts

def html(self, counters=None):
def html(self):
if len(self.tests) == 0:
return "\n"
return '<ul class="tests">%s</ul>' % (
Expand Down Expand Up @@ -302,8 +301,7 @@ def __init__(

if text.count("<dl>") != text.count("</dl>"):
raise ValueError(
"Missing opening or closing <dl> tag in "
"{} documentation".format(title)
f"Missing opening or closing <dl> tag in {title} documentation"
)

# Needs to come after self.chapter is initialized since
Expand Down Expand Up @@ -364,8 +362,7 @@ def __init__(

if text.count("<dl>") != text.count("</dl>"):
raise ValueError(
"Missing opening or closing <dl> tag in "
"{} documentation".format(title)
f"Missing opening or closing <dl> tag in {title} documentation"
)
# print("YYY Adding section", title)
chapter.sections_by_slug[self.slug] = self
Expand Down Expand Up @@ -450,8 +447,7 @@ def __init__(

if text.count("<dl>") != text.count("</dl>"):
raise ValueError(
"Missing opening or closing <dl> tag in "
"{} documentation".format(title)
f"Missing opening or closing <dl> tag in {title} documentation"
)
self.section.subsections_by_slug[self.slug] = self

Expand Down Expand Up @@ -520,7 +516,7 @@ def html(self) -> str:


class DjangoDocTests(DocTests):
def html(self, counters=None):
def html(self):
if len(self.tests) == 0:
return "\n"
return '<ul class="tests">%s</ul>' % (
Expand All @@ -531,6 +527,6 @@ def html(self, counters=None):


class DjangoDocText(DocText):
def html(self, counters=None) -> str:
result = escape_html(self.text, counters=counters)
def html(self) -> str:
result = escape_html(self.text)
return result
2 changes: 1 addition & 1 deletion mathics_django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_bool_from_environment(env_var: str, default_value: str):
)

# We need another version as a fallback, and that is distributed with the
# package. It is note user writable and not in the user space.
# package. It is not user writable and not in the user space.
DOC_SYSTEM_HTML_DATA_PATH = os.environ.get(
"DOC_SYSTEM_HTML_DATA_PATH", osp.join(ROOT_DIR, "doc", "doc_html_data.pcl")
)
Expand Down
51 changes: 43 additions & 8 deletions mathics_django/web/controllers/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from django.core.handlers.wsgi import WSGIRequest
from django.http import Http404, HttpResponse
from django.shortcuts import render
from mathics.eval.pymathics import pymathics_modules
from mathics.doc.common_doc import get_module_doc
from mathics.eval.pymathics import pymathics_builtins_by_module, pymathics_modules

from mathics_django.doc import documentation
from mathics_django.doc.django_doc import (
Expand All @@ -21,12 +22,47 @@

DocResponse = Union[HttpResponse, JsonResponse]

seen_pymathics_modules = copy(pymathics_modules)



def check_for_pymathics_load():
global seen_pymathics_modules
if seen_pymathics_modules != pymathics_modules:
# print("XXX refresh pymathics doc")
global documentation
documentation = MathicsDjangoDocumentation()
print("XXX refresh pymathics doc", pymathics_modules)
new_modules = pymathics_modules - seen_pymathics_modules
for new_module in new_modules:
title, _ = get_module_doc(new_module)
mathics3_module_part = documentation.parts_by_slug.get("Mathics3 Modules", None)
# If this is the first loaded module, we need to create the Part
if mathics3_module_part is None:
mathics3_module_part = self.doc_part(
"Mathics3 Modules", pymathics_modules, pymathics_builtins_by_module, True
)
seen_pymathics_modules = copy(pymathics_modules)
return

# The part already exists. Lets add the new chapter.

chapter = mathics3_module_part.doc.gather_chapter_doc_fn(
mathics3_module_part,
title,
mathics3_module_part.doc,
)
from trepan.api import debug

debug()
submodule_names_seen = set()
chapter.doc.doc_chapter(
new_module,
mathics3_module_part,
pymathics_builtins_by_module,
seen_pymathics_modules,
submodule_names_seen,
)
chapter.get_tests()
seen_pymathics_modules = copy(pymathics_modules)
pass


def doc(request: WSGIRequest, ajax: bool = False) -> DocResponse:
Expand All @@ -44,7 +80,9 @@ def doc(request: WSGIRequest, ajax: bool = False) -> DocResponse:

def doc_chapter(request: WSGIRequest, part, chapter, ajax: bool = False) -> DocResponse:
"""
Produces HTML via jinja templating for a chapter. Some examples of Chapters:
Produces HTML via jinja templating for a chapter. Some examples of
Chapters:
* Introduction (in part Manual)
* Procedural Programming (in part Reference of Built-in Symbols)
"""
Expand Down Expand Up @@ -87,9 +125,6 @@ def doc_part(request: WSGIRequest, part, ajax: bool = False) -> DocResponse:
)


seen_pymathics_modules = copy(pymathics_modules)


def doc_search(request: WSGIRequest) -> DocResponse:
check_for_pymathics_load()
query = request.GET.get("query", "")
Expand Down
2 changes: 1 addition & 1 deletion mathics_django/web/templates/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ <h1>Connection Information</h1>

<p>Mathics3 is a general-purpose computer algebra system.</p>

<p>Copyright (C) 2021-2023 The Mathics3 Team<p><br>
<p>Copyright (C) 2021-2024 The Mathics3 Team<p><br>

This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it
Expand Down

0 comments on commit 9572a48

Please sign in to comment.