chiark / gitweb /
documentation/python: don't die on reST errors in pages.
authorVladimír Vondruš <mosra@centrum.cz>
Thu, 5 Sep 2019 18:20:34 +0000 (20:20 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Fri, 6 Sep 2019 00:10:03 +0000 (02:10 +0200)
documentation/python.py
documentation/test_python/page/error.html [new file with mode: 0644]
documentation/test_python/page/error.rst [new file with mode: 0644]
documentation/test_python/page/pages.html
documentation/test_python/test_page.py

index 121e17186fbd6fd521551f8dff9c0517738ac278..3e0390176323c81f7779b3ce2663cdd7d9cc5354 100755 (executable)
@@ -2087,8 +2087,32 @@ def render_page(state: State, path, input_filename, env):
     for hook in state.hooks_pre_page:
         hook(path=path)
 
+    page = Empty()
+    page.filename = filename
+    page.url = url
+    page.prefix_wbr = path[0]
+
     # Render the file
-    with open(input_filename, 'r') as f: pub = publish_rst(state, f.read(), source_path=input_filename)
+    with open(input_filename, 'r') as f:
+        try:
+            pub = publish_rst(state, f.read(), source_path=input_filename)
+        except docutils.utils.SystemMessage:
+            logging.error("Failed to process %s, rendering an empty page", input_filename)
+
+            # Empty values for fields expected by other code
+            page.breadcrumb = [(os.path.basename(input_filename), url)]
+            page.summary = ''
+            page.content = ''
+            entry = state.name_map['.'.join(path)]
+            entry.summary = page.summary
+            entry.name = page.breadcrumb[-1][0]
+            render(config=state.config,
+                template='page.html',
+                filename=page.filename,
+                url=page.url,
+                env=env,
+                page=page)
+            return
 
     # Extract metadata from the page
     metadata = {}
@@ -2113,13 +2137,7 @@ def render_page(state: State, path, input_filename, env):
 
     # Breadcrumb, we don't do page hierarchy yet
     assert len(path) == 1
-    breadcrumb = [(pub.writer.parts.get('title'), url)]
-
-    page = Empty()
-    page.filename = filename
-    page.url = url
-    page.breadcrumb = breadcrumb
-    page.prefix_wbr = path[0]
+    page.breadcrumb = [(pub.writer.parts.get('title'), url)]
 
     # Set page content and add extra metadata from there
     page.content = pub.writer.parts.get('body').rstrip()
@@ -2130,7 +2148,7 @@ def render_page(state: State, path, input_filename, env):
     # for index
     entry = state.name_map['.'.join(path)]
     entry.summary = page.summary
-    entry.name = breadcrumb[-1][0]
+    entry.name = page.breadcrumb[-1][0]
 
     if not state.config['SEARCH_DISABLED']:
         result = Empty()
diff --git a/documentation/test_python/page/error.html b/documentation/test_python/page/error.html
new file mode 100644 (file)
index 0000000..86a278d
--- /dev/null
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8" />
+  <title>error.rst | My Python Project</title>
+  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600" />
+  <link rel="stylesheet" href="m-dark+documentation.compiled.css" />
+  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+</head>
+<body>
+<header><nav id="navigation">
+  <div class="m-container">
+    <div class="m-row">
+      <a href="index.html" id="m-navbar-brand" class="m-col-t-8 m-col-m-none m-left-m">My Python Project</a>
+    </div>
+  </div>
+</nav></header>
+<main><article>
+  <div class="m-container m-container-inflatable">
+    <div class="m-row">
+      <div class="m-col-l-10 m-push-l-1">
+        <h1>
+          error.rst
+        </h1>
+      </div>
+    </div>
+  </div>
+</article></main>
+</body>
+</html>
diff --git a/documentation/test_python/page/error.rst b/documentation/test_python/page/error.rst
new file mode 100644 (file)
index 0000000..5cd238b
--- /dev/null
@@ -0,0 +1,7 @@
+This page has a title
+#####################
+
+But it
+    also
+        has
+    errors.
index 6caa460fe83013c0d2f9d0dcc0bd05e0dea52fcf..86a69c9f30e5e21e0f1330f94fb7e335397d5ac7 100644 (file)
@@ -22,6 +22,7 @@
         <h1>Pages</h2>
         <ul class="m-doc">
           <li><a href="another.html" class="m-doc">Another page</a> <span class="m-doc">Here's some summary. <strong>It's formated as well.</strong></span></li>
+          <li><a href="error.html" class="m-doc">error.rst</a> <span class="m-doc"></span></li>
         </ul>
         <script>
         function toggle(e) {
index 980d7d09ec447c93b3a5279536ad2404029087b5..41422605ad57c3e77eba71f2d7a9b69fe6def622 100644 (file)
@@ -37,10 +37,11 @@ def dot_version():
 class Page(BaseTestCase):
     def test(self):
         self.run_python({
-            'INPUT_PAGES': ['index.rst', 'another.rst']
+            'INPUT_PAGES': ['index.rst', 'another.rst', 'error.rst']
         })
         self.assertEqual(*self.actual_expected_contents('index.html'))
         self.assertEqual(*self.actual_expected_contents('another.html'))
+        self.assertEqual(*self.actual_expected_contents('error.html'))
         self.assertEqual(*self.actual_expected_contents('pages.html'))
 
 class InputSubdir(BaseTestCase):