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

Add date in template context and make it possible to build one branch directly into the outputdir flag. #136

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sphinx_multiversion/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def load_sphinx_config_worker(q, confpath, confoverrides, add_defaults):
"html",
str,
)
current_config.add(
"smv_toplevelref",
sphinx.DEFAULT_TOPLEVELREF,
"html",
str,
)
current_config.add("smv_prefer_remote_refs", False, "html", bool)
current_config.pre_init_values()
current_config.init_values()
Expand Down Expand Up @@ -280,6 +286,10 @@ def main(argv=None):
ref=gitref,
config=current_config,
)

if gitref.name == config.smv_toplevelref:
outputdir = ""

if outputdir in outputdirs:
logger.warning(
"outputdir '%s' for %s conflicts with other versions",
Expand Down
14 changes: 14 additions & 0 deletions sphinx_multiversion/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
DEFAULT_REMOTE_WHITELIST = None
DEFAULT_RELEASED_PATTERN = r"^tags/.*$"
DEFAULT_OUTPUTDIR_FORMAT = r"{ref.name}"
DEFAULT_TOPLEVELREF = None

Version = collections.namedtuple(
"Version",
Expand All @@ -53,6 +54,7 @@
"version",
"release",
"is_released",
"creatordate",
],
)

Expand All @@ -71,6 +73,7 @@ def _dict_to_versionobj(self, v):
version=v["version"],
release=v["release"],
is_released=v["is_released"],
creatordate=v["creatordate"],
)

@property
Expand Down Expand Up @@ -165,6 +168,13 @@ def vpathto(self, other_version_name):
)


def format_date(value, format="%Y-%m-%d"):
date_obj = datetime.datetime.strptime(
value, DATE_FMT
) # Convert the date string to a datetime object
return date_obj.strftime(format) # Format to desired output


def html_page_context(app, pagename, templatename, context, doctree):
versioninfo = VersionInfo(
app, context, app.config.smv_metadata, app.config.smv_current_version
Expand All @@ -177,6 +187,9 @@ def html_page_context(app, pagename, templatename, context, doctree):
context["latest_version"] = versioninfo[app.config.smv_latest_version]
context["html_theme"] = app.config.html_theme

# Add a filter to format the date
app.builder.templates.environment.filters["format_date"] = format_date


def config_inited(app, config):
"""Update the Sphinx builder.
Expand Down Expand Up @@ -236,6 +249,7 @@ def setup(app):
app.add_config_value(
"smv_outputdir_format", DEFAULT_OUTPUTDIR_FORMAT, "html"
)
app.add_config_value("smv_toplevelref", DEFAULT_TOPLEVELREF, "html")
app.connect("config-inited", config_inited)

return {
Expand Down