Coverage for C:\leo.repo\leo-editor\leo\plugins\importers\ctext.py: 19%
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:tbrown.20140801105909.47549: * @file ../plugins/importers/ctext.py
3#@@language python
4#@@tabwidth -4
5from typing import List
6from leo.plugins.importers import linescanner
7Importer = linescanner.Importer
8#@+others
9#@+node:tbrown.20140801105909.47551: ** class CText_Importer
10class CText_Importer(Importer):
11 #@+<< ctext docstring >>
12 #@+node:ekr.20161130053507.1: *3* << ctext docstring >>
13 """
14 Read/Write simple text files with hierarchy embedded in headlines::
16 Leading text in root node of subtree
18 Etc. etc.
20 ### A level one node #####################################
22 This would be the text in this level one node.
24 And this.
26 ### Another level one node ###############################
28 Another one
30 #### A level 2 node ######################################
32 See what we did there - one more '#' - this is a subnode.
34 Leading / trailing whitespace may not be preserved. '-' and '/'
35 are used in place of '#' for SQL and JavaScript.
37 """
38 #@-<< ctext docstring >>
39 #@+others
40 #@+node:ekr.20161130053335.1: *3* ctext_i.__init__
41 def __init__(self, importCommands, **kwargs):
42 """Ctor for CoffeeScriptScanner class."""
43 super().__init__(
44 importCommands,
45 language='ctext',
46 state_class=None,
47 strict=False
48 )
49 self.fileType = importCommands.fileType
50 #@+node:tbrown.20140801105909.47552: *3* ctext_i.write_lines
51 def write_lines(self, node, lines):
52 """write the body lines to body normalizing whitespace"""
53 node.b = '\n'.join(lines).strip('\n') + '\n'
54 lines[:] = []
55 #@+node:tbrown.20140801105909.47553: *3* ctext_i.run
56 def run(self, s, parent, parse_body=False):
57 """Override Importer.run()"""
58 # c = self.c
59 root = parent.copy()
60 cchar = '#'
61 if self.fileType.lower() == '.tex':
62 cchar = '%'
63 if self.fileType.lower() == '.sql':
64 cchar = '-'
65 if self.fileType.lower() == '.js':
66 cchar = '/'
67 level = -1
68 nd = parent.copy()
69 lines: List[str] = []
70 for line in s.split('\n'):
71 if line.startswith(cchar * 3):
72 word = line.split()
73 if word[0].strip(cchar) == '':
74 self.write_lines(nd, lines)
75 new_level = len(word[0]) - 3
76 if new_level > level:
77 # go down one level
78 level = new_level
79 nd = nd.insertAsLastChild()
80 nd.h = ' '.join(word[1:]).strip(cchar + ' ')
81 else:
82 # go up zero or more levels
83 while level > new_level and level > 0:
84 level -= 1
85 nd = nd.parent()
86 nd = nd.insertAfter()
87 nd.h = ' '.join(word[1:]).strip(cchar + ' ')
88 else:
89 lines.append(line)
90 self.write_lines(nd, lines)
91 # It's always useless for an an import to dirty the outline.
92 for p in root.self_and_subtree():
93 p.clearDirty()
94 # #1451: The caller should be responsible for this.
95 # if changed:
96 # c.setChanged()
97 # else:
98 # c.clearChanged()
99 return True
100 #@-others
101#@-others
102importer_dict = {
103 '@auto': ['@auto-ctext',],
104 'func': CText_Importer.do_import(),
105}
106#@@language python
107#@@tabwidth -4
108#@-leo