Coverage for C:\leo.repo\leo-editor\leo\plugins\writers\leo_rst.py: 31%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#@+leo-ver=5-thin
2#@+node:ekr.20140726091031.18080: * @file ../plugins/writers/leo_rst.py
3"""
4The write code for @auto-rst and other reStructuredText nodes.
5This is very different from rst3's write code.
7This module must **not** be named rst, so as not to conflict with docutils.
8"""
9# pylint: disable=unused-import
10from leo.core import leoGlobals as g
11import leo.plugins.writers.basewriter as basewriter
12import leo.plugins.importers.leo_rst as rst_importer
13# Make *sure* that reader's underlines match the writer's.
14underlines = rst_importer.underlines
15#@+others
16#@+node:ekr.20140726091031.18092: ** class RstWriter
17class RstWriter(basewriter.BaseWriter):
18 """
19 The writer class for @auto-rst and other reStructuredText nodes.
20 This is *very* different from rst3 command's write code.
21 """
22 # def __init__(self,c):
23 # super().__init__(c)
24 #@+others
25 #@+node:ekr.20140726091031.18150: *3* rstw.underline_char
26 def underline_char(self, p, root_level):
27 """Return the underlining character for position p."""
28 # OLD underlines = '=+*^~"\'`-:><_'
29 # OLD underlines = "!\"$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
30 # '#' is reserved.
31 i = p.level() - root_level
32 return underlines[min(i, len(underlines) - 1)]
33 #@+node:ekr.20140726091031.18089: *3* rstw.write
34 def write(self, root):
35 """Write an @auto tree containing imported rST code."""
36 root_level = root.level()
37 self.write_root(root)
38 for p in root.subtree():
39 if hasattr(self.at, 'force_sentinels'):
40 self.put_node_sentinel(p, '.. ')
41 ch = self.underline_char(p, root_level)
42 # Put the underlined headline
43 self.put(p.h)
44 # Fix #242: @auto-rst open/save error.
45 n = max(4, len(g.toEncodedString(p.h, reportErrors=False)))
46 self.put(ch * n)
47 # Ensure that every section ends with exactly two newlines.
48 s = p.b.rstrip() + '\n\n'
49 lines = s.splitlines(False)
50 if lines and lines[0].strip():
51 self.put('')
52 # Put the body.
53 for s in lines:
54 self.put(s)
55 root.setVisited()
56 return True
57 #@+node:ekr.20171230165645.1: *3* rstw.write_root
58 def write_root(self, root):
59 """Write the root @auto-org node."""
60 lines = [z for z in g.splitLines(root.b) if not g.isDirective(z)]
61 for s in lines:
62 self.put(s)
63 #@-others
64#@-others
65writer_dict = {
66 '@auto': ['@auto-rst',],
67 'class': RstWriter,
68 'extensions': ['.rst', '.rest',],
69}
70#@@language python
71#@@tabwidth -4
72#@-leo