mkdocs-include-markdown-plugin 7.1.1__py3-none-any.whl → 7.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,9 +9,6 @@ import time
9
9
  from importlib.util import find_spec
10
10
 
11
11
 
12
- CACHE_AVAILABLE = find_spec('platformdirs') is not None
13
-
14
-
15
12
  class Cache:
16
13
  """Cache for arbitrary content, one file per entry."""
17
14
 
@@ -64,31 +61,52 @@ class Cache:
64
61
  def clean(self) -> None:
65
62
  """Clean expired entries from the cache."""
66
63
  for fname in os.listdir(self.cache_dir):
64
+ if fname == '.gitignore':
65
+ continue
67
66
  fpath = os.path.join(self.cache_dir, fname)
68
67
  creation_time = self.get_creation_time_from_fpath(fpath)
69
68
  if time.time() > creation_time + self.expiration_seconds:
70
69
  os.remove(fpath)
71
70
 
72
71
 
73
- def get_cache_directory() -> str | None:
74
- """Get the cache directory."""
75
- if not CACHE_AVAILABLE:
72
+ def get_cache_directory(cache_dir: str) -> str | None:
73
+ """Get cache directory."""
74
+ if cache_dir:
75
+ return cache_dir
76
+
77
+ if not is_platformdirs_installed():
76
78
  return None
77
79
 
78
80
  try:
79
81
  from platformdirs import user_data_dir
80
82
  except ImportError:
81
83
  return None
84
+ else:
85
+ return user_data_dir('mkdocs-include-markdown-plugin')
86
+
87
+
88
+ def initialize_cache(expiration_seconds: int, cache_dir: str) -> Cache | None:
89
+ """Initialize a cache instance."""
90
+ cache_directory = get_cache_directory(cache_dir)
91
+
92
+ if cache_directory is None:
93
+ return None
82
94
 
83
- cache_dir = user_data_dir('mkdocs-include-markdown-plugin')
84
- os.makedirs(cache_dir, exist_ok=True)
95
+ os.makedirs(cache_directory, exist_ok=True)
85
96
 
86
- return cache_dir
97
+ # Add a `.gitignore` file to prevent the cache directory from being
98
+ # included in the repository. This is needed because the cache directory
99
+ # can be configured as a relative path with `cache_dir` setting.
100
+ gitignore = os.path.join(cache_directory, '.gitignore')
101
+ if not os.path.exists(gitignore):
102
+ with open(gitignore, 'wb') as f:
103
+ f.write(b'*\n')
87
104
 
105
+ cache = Cache(cache_directory, expiration_seconds)
106
+ cache.clean()
107
+ return cache
88
108
 
89
- def initialize_cache(expiration_seconds: int) -> Cache | None:
90
- """Initialize a cache instance."""
91
- cache_dir = get_cache_directory()
92
- return None if cache_dir is None else Cache(
93
- cache_dir, expiration_seconds,
94
- )
109
+
110
+ def is_platformdirs_installed() -> bool:
111
+ """Check if `platformdirs` package is installed without importing it."""
112
+ return find_spec('platformdirs') is not None
@@ -24,6 +24,7 @@ class PluginConfig(Config): # noqa: D101
24
24
  end = Optional(MkType(str))
25
25
  exclude = ListOfItems(MkType(str), default=[])
26
26
  cache = MkType(int, default=0)
27
+ cache_dir = MkType(str, default='')
27
28
  recursive = MkType(bool, default=True)
28
29
  directives = MkType(
29
30
  dict,
@@ -31,22 +31,19 @@ class IncludeMarkdownPlugin(BasePlugin[PluginConfig]):
31
31
 
32
32
  def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
33
33
  if self.config.cache > 0:
34
- cache = initialize_cache(self.config.cache)
34
+ cache = initialize_cache(self.config.cache, self.config.cache_dir)
35
35
  if cache is None:
36
36
  raise PluginError(
37
- 'The "platformdirs" package is required to use the'
38
- ' "cache" option. Install'
39
- ' mkdocs-include-markdown-plugin with the "cache"'
40
- ' extra to install it.',
37
+ 'Either `cache_dir` global setting must be configured or'
38
+ ' `platformdirs` package is required to use the'
39
+ ' `cache` option. Install mkdocs-include-markdown-plugin'
40
+ " with the 'cache' extra to install `platformdirs`.",
41
41
  )
42
- cache.clean()
43
42
  self._cache = cache
44
43
 
45
44
  if '__default' not in self.config.directives: # pragma: no cover
46
45
  for directive in self.config.directives:
47
- if directive not in {
48
- 'include', 'include-markdown',
49
- }:
46
+ if directive not in ('include', 'include-markdown'):
50
47
  raise PluginError(
51
48
  f"Invalid directive name '{directive}' at 'directives'"
52
49
  ' global setting. Valid values are "include" and'
@@ -244,12 +244,9 @@ def rewrite_relative_urls(
244
244
  ``source_path`` will still work when inserted into a file at
245
245
  ``destination_path``.
246
246
  """
247
- def rewrite_url(url: str) -> str:
248
- from urllib.parse import urlparse, urlunparse
249
-
250
- if is_relative_path(url):
251
- return url
247
+ from urllib.parse import urlparse, urlunparse
252
248
 
249
+ def rewrite_url(url: str) -> str:
253
250
  scheme, netloc, path, params, query, fragment = urlparse(url)
254
251
 
255
252
  # absolute or mail
@@ -425,7 +422,7 @@ def increase_headings_offset(markdown: str, offset: int = 0) -> str:
425
422
 
426
423
  def rstrip_trailing_newlines(content: str) -> str:
427
424
  """Removes trailing newlines from a string."""
428
- while content.endswith('\n') or content.endswith('\r'):
425
+ while content.endswith(('\n', '\r')):
429
426
  content = content.rstrip('\r\n')
430
427
  return content
431
428
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: mkdocs-include-markdown-plugin
3
- Version: 7.1.1
3
+ Version: 7.1.2
4
4
  Summary: Mkdocs Markdown includer plugin.
5
5
  Project-URL: Source, https://github.com/mondeja/mkdocs-include-markdown-plugin
6
6
  Project-URL: Documentation, https://github.com/mondeja/mkdocs-include-markdown-plugin#documentation
@@ -135,8 +135,9 @@ plugins:
135
135
  cache: 600
136
136
  ```
137
137
 
138
- In order to use this feature, the dependency [platformdirs] must be installed.
139
- You can include it in the installation of the plugin adding the `cache` extra:
138
+ In order to use this feature, the dependency [platformdirs] must be installed
139
+ or the setting [`cache_dir`](#cache_dir) must be defined. You can include
140
+ [platformdirs] in the installation of the plugin adding the `cache` extra:
140
141
 
141
142
  ```txt
142
143
  # requirements.txt
@@ -145,6 +146,23 @@ mkdocs-include-markdown-plugin[cache]
145
146
 
146
147
  <!-- mdpo-disable-next-line -->
147
148
 
149
+ #### `cache_dir`
150
+
151
+ Directory where cached HTTP requests will be stored. If set, [platformdirs] is not
152
+ needed to be installed to use [`cache`](#cache).
153
+
154
+ ```yaml
155
+ plugins:
156
+ - include-markdown:
157
+ cache: 600
158
+ cache_dir: ./mkdocs-include-markdown-cache
159
+ ```
160
+
161
+ A _.gitignore_ file will be added to the cache directory if not exists to avoid
162
+ committing the cache files.
163
+
164
+ <!-- mdpo-disable-next-line -->
165
+
148
166
  #### `directives`
149
167
 
150
168
  Customize the names of the directives.
@@ -1,15 +1,15 @@
1
1
  mkdocs_include_markdown_plugin/__init__.py,sha256=ByOde15RQSHlZTmkepPSzIt0GzUIv_-GKORFDnxnOmc,193
2
- mkdocs_include_markdown_plugin/cache.py,sha256=9MMQLIRsLRW2ezh4pfOPr0fFz8ayqS13lvm7foMvMRI,3078
3
- mkdocs_include_markdown_plugin/config.py,sha256=4jrqWLws644tw247uCk2AXE2z6x3x30ipdtmKF-q2po,1051
2
+ mkdocs_include_markdown_plugin/cache.py,sha256=j4GWPEMOj8jVQrJRXryT-r3TNzWb3mFLE6FCROe_1MQ,3778
3
+ mkdocs_include_markdown_plugin/config.py,sha256=VX95U8-9gFzPUIBSTA8TSL4xOD49qTMSs8dhFiilGns,1091
4
4
  mkdocs_include_markdown_plugin/directive.py,sha256=1OEA1XZKlYRvScOAP0gmaSROLmbWXTPbTJgPELk71yg,10455
5
5
  mkdocs_include_markdown_plugin/event.py,sha256=nT55KnM2gmzVKmlDS8SEBUDbOJ8TDXRNx0k_qhB5w4w,24507
6
6
  mkdocs_include_markdown_plugin/files_watcher.py,sha256=hu-63u8syl00mXP59vP_iqD7T1R9iU9JjiZOHDV5fas,298
7
7
  mkdocs_include_markdown_plugin/logger.py,sha256=nl63G4IJSt5mEYHbObuh7BmmowSv-aa1l_v9I257570,230
8
- mkdocs_include_markdown_plugin/plugin.py,sha256=3aaROECzm8Xa_fIKf33-iA9QU745E5fw3RytIBANzsw,3828
9
- mkdocs_include_markdown_plugin/process.py,sha256=Q1gMYW50D11o61Uv1w9Yz4fnRRPjHqaUdYD1Q1OZ-Jo,17859
8
+ mkdocs_include_markdown_plugin/plugin.py,sha256=0muHIhmklC7v-QKWsAcAtZKhhZQ2olH6b28b3_3e4gI,3850
9
+ mkdocs_include_markdown_plugin/process.py,sha256=6mVGPDSMtnuUE5IF05GUwef0PYPUhI9G2FIX5nFHqcU,17779
10
10
  mkdocs_include_markdown_plugin/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- mkdocs_include_markdown_plugin-7.1.1.dist-info/METADATA,sha256=ia2-TtKYDYt5s7BhQ8Q3U8tdnN8B0KdMv3dBMIpCYck,12265
12
- mkdocs_include_markdown_plugin-7.1.1.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
13
- mkdocs_include_markdown_plugin-7.1.1.dist-info/entry_points.txt,sha256=WEL5m7sw5ZMBwmdOs5S_9mGoZfLCaUDPkDKw7CUk16Q,96
14
- mkdocs_include_markdown_plugin-7.1.1.dist-info/licenses/LICENSE,sha256=OunyKlPxiuvLB3ENbo2nyV-OPndkwzyZ5Gzwei0ofbc,11365
15
- mkdocs_include_markdown_plugin-7.1.1.dist-info/RECORD,,
11
+ mkdocs_include_markdown_plugin-7.1.2.dist-info/METADATA,sha256=Y0FPqVd0VcmyctgO8jJK89QQxlBvGDg09oqwZPr_U30,12737
12
+ mkdocs_include_markdown_plugin-7.1.2.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
13
+ mkdocs_include_markdown_plugin-7.1.2.dist-info/entry_points.txt,sha256=WEL5m7sw5ZMBwmdOs5S_9mGoZfLCaUDPkDKw7CUk16Q,96
14
+ mkdocs_include_markdown_plugin-7.1.2.dist-info/licenses/LICENSE,sha256=OunyKlPxiuvLB3ENbo2nyV-OPndkwzyZ5Gzwei0ofbc,11365
15
+ mkdocs_include_markdown_plugin-7.1.2.dist-info/RECORD,,