Skip to content

Commit 9186fe6

Browse files
author
Laurent Franceschetti
committed
Fixed null test and completed added opt_in and opt_out
- changed name from opt-in and opt-out
1 parent b76efef commit 9186fe6

File tree

19 files changed

+160
-18
lines changed

19 files changed

+160
-18
lines changed

mkdocs_macros/plugin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ def on_page_markdown(self, markdown, page:Page,
950950
"""
951951
self._page = page
952952
if not self.variables:
953-
return markdown
953+
self.markdown = markdown
954954
else:
955955
trace("Rendering source page:", page.file.src_path)
956956
# Update the page info in the document
@@ -986,12 +986,12 @@ def on_page_markdown(self, markdown, page:Page,
986986
for func in self.post_macro_functions:
987987
func(self)
988988

989-
# save the rendered page, with its YAML header
990-
if get_log_level('DEBUG'):
991-
self._save_debug_file(page,
992-
rendered_markdown=self.markdown)
989+
# save the rendered page, with its YAML header
990+
if get_log_level('DEBUG'):
991+
self._save_debug_file(page,
992+
rendered_markdown=self.markdown)
993993

994-
return self.markdown
994+
return self.markdown
995995

996996
def on_post_build(self, config: config_options.Config):
997997
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Initialization
1313
# --------------------
1414

15-
VERSION_NUMBER = '1.3.0'
15+
VERSION_NUMBER = '1.3.1'
1616

1717
# required if you want to run document/test
1818
# pip install 'mkdocs-macros-plugin[test]'

test/fixture.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing import List
1515
import json
1616
from typing import Any, List
17-
import difflib
17+
import inspect
1818

1919

2020
# from rich import print
@@ -306,12 +306,25 @@ def get_tables(markdown_text:str) -> dict[pd.DataFrame]:
306306

307307

308308

309-
309+
# ---------------------------
310+
# OS Functions
311+
# ---------------------------
310312
def run_command(command, *args) -> subprocess.CompletedProcess:
311313
"Execute a command"
312314
full_command = [command] + list(args)
313315
return subprocess.run(full_command, capture_output=True, text=True)
314316

317+
def get_caller_directory():
318+
"Get the caller's directory name (to be called from a function)"
319+
# Get the current frame
320+
current_frame = inspect.currentframe()
321+
# Get the caller's frame
322+
caller_frame = inspect.getouterframes(current_frame, 2)
323+
# Get the file name of the caller
324+
caller_file = caller_frame[1].filename
325+
# Get the absolute path of the directory containing the caller file
326+
directory_abspath = os.path.abspath(os.path.dirname(caller_file))
327+
return directory_abspath
315328

316329
# ---------------------------
317330
# Log parsing
@@ -523,9 +536,15 @@ def is_rendered(self) -> bool:
523536
or in `on_post_page_macro()`) but the text itself
524537
was not modified.
525538
"""
526-
return self.source_page.markdown not in self.markdown
539+
# make sure that the source is stripped, to be sure.
540+
return self.source_page.markdown.strip() not in self.markdown
527541

528542

543+
def __repr__(self):
544+
"""
545+
Important for error printout
546+
"""
547+
return f"Markdown page ({self.filename}):\n{self.text}"
529548

530549
# ---------------------------
531550
# Main class
@@ -535,7 +554,11 @@ class DocProject(object):
535554

536555
def __init__(self, directory:str=''):
537556
"Initialize"
538-
self._project_dir = os.path.join(REF_DIR, directory)
557+
project_dir = os.path.join(REF_DIR, directory)
558+
if not os.path.isdir(project_dir):
559+
raise FileNotFoundError(f"Doc directory '{directory}' "
560+
"does not exist.")
561+
self._project_dir = project_dir
539562
# test existence of YAML file or fail
540563
self.config_file
541564

@@ -802,9 +825,23 @@ def pages(self) -> List[TestMarkdownPage]:
802825

803826
def get_page(self, name:str):
804827
"Get the page by its filename or a substring"
828+
print("SEARCHING:", name)
805829
for page in self.pages:
806-
if name in page.filename:
830+
# give priority to exact matches
831+
if name == page.filename:
832+
return page
833+
# try without extension
834+
stem, _ = os.path.splitext(page.filename)
835+
if name == stem:
836+
return page
837+
# try again without full path
838+
for page in self.pages:
839+
if page.filename.endswith(name):
840+
return page
841+
stem, _ = os.path.splitext(page.filename)
842+
if stem.endswith(name):
807843
return page
844+
print("- NOT FOUND")
808845

809846
def get_plugin(self, name:str) -> SuperDict:
810847
"Get the plugin by its plugin name"

test/null/test_site.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from test.fixture import DocProject
1111

12-
CURRENT_PROJECT = 'simple'
12+
CURRENT_PROJECT = 'null'
1313

1414

1515

@@ -27,7 +27,8 @@ def test_pages():
2727

2828

2929
page = PROJECT.get_page('index')
30-
assert not page.is_rendered
30+
ERROR_MSG = f"Is rendered!:\n{page.markdown}\n---SOURCE:\n{page.source_page.markdown}\n---"
31+
assert not page.is_rendered, ERROR_MSG
3132
assert not page.has_error
3233

3334

test/opt_in/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
This __init__.py file is indispensable for pytest to
3+
recognize its packages.
4+
"""
File renamed without changes.
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
---
22
title: Opt-in by paths
3+
signal: Rendered!!!
34
---
45
# {{ title }}
56

7+
The signal is: {{ signal }}.
8+
69
This page must be rendered because there is `render_*.md` in the
710
`force_render_paths` variable, and the name of this page is
811
**`{{page.file.src_uri}}`**.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
render_macros: false # opt-in
2+
render_macros: false # opt-out
33
---
44
# Opt-out by page header (priority)
55

0 commit comments

Comments
 (0)