Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize nltk.org/howto pages #2856

Merged
merged 3 commits into from Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -40,6 +40,7 @@ coverage.xml

# automatically built files for website
web/api/*.rst
web/howto/*.rst

# iPython notebooks

Expand Down
2 changes: 1 addition & 1 deletion nltk/test/corpus.doctest
Expand Up @@ -25,7 +25,7 @@ and new corpus reader classes. Section `Regression Tests`_
and associated functions and classes.

.. contents:: **Table of Contents**
:depth: 2
:depth: 4
:backlinks: none

---------------------
Expand Down
5 changes: 5 additions & 0 deletions web/_templates/doctest.rst
@@ -0,0 +1,5 @@
{% for item in range(17 + module_name|length) -%}#{%- endfor %}
Sample usage for {{ module_name }}
{% for item in range(17 + module_name|length) -%}#{%- endfor %}

.. include:: ../../nltk/test/{{ module_name }}.doctest
40 changes: 40 additions & 0 deletions web/conf.py
Expand Up @@ -54,6 +54,46 @@ def run_apidoc(app):
)


def generate_howto():
"""Custom function for generating contents in the ``howto`` folder,
based on the ``ntlk/test/*.doctest`` files.
"""
import glob
import re

from jinja2 import Template

modules = []

web_folder = os.path.dirname(os.path.abspath(__file__))
howto_folder = os.path.join(web_folder, "howto")
if not os.path.exists(howto_folder):
os.makedirs(howto_folder)

# Load jinja template
with open(os.path.join(web_folder, "_templates", "doctest.rst")) as f:
doctest_template = Template(f.read())

print("Generating HOWTO pages...")
# Iterate over .doctest files, and find the module_name.
pattern = re.compile(r"(\w+)\.doctest$")
for path in glob.glob(os.path.join(web_folder, "..", "nltk", "test", "*.doctest")):
match = pattern.search(path)
module_name = match.group(1)
# Ignore index.doctest, we already have an index, i.e. howto.rst
if module_name == "index":
continue
# Write .rst files based on the doctest_template.
doctest_template.stream(module_name=module_name).dump(
os.path.join(howto_folder, f"{module_name}.rst")
)
modules.append(module_name)

print(f"Generated {len(modules)} HOWTO pages.")


generate_howto()

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

Expand Down
8 changes: 8 additions & 0 deletions web/howto.rst
@@ -0,0 +1,8 @@
Example usage of NLTK modules
=============================

.. toctree::
:titlesonly:
:glob:

howto/*
2 changes: 1 addition & 1 deletion web/index.rst
Expand Up @@ -76,7 +76,7 @@ Next Steps
:caption: NLTK Documentation

API Reference <api/nltk>
Example Usage <https://www.nltk.org/howto>
Example Usage <howto>
Module Index <py-modindex>
Wiki <https://github.com/nltk/nltk/wiki>
FAQ <https://github.com/nltk/nltk/wiki/FAQ>
Expand Down