Conditions | 16 |
Total Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like listof() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python |
||
115 | def listof(key, value, format, meta): |
||
116 | global headers2 |
||
117 | |||
118 | # Is it a header? |
||
119 | if key == 'Header': |
||
120 | [level, [id, classes, attributes], content] = value |
||
121 | if 'unnumbered' not in classes: |
||
122 | headers2[level - 1] = headers2[level - 1] + 1 |
||
123 | for index in range(level, 6): |
||
124 | headers2[index] = 0 |
||
125 | |||
126 | # Is it a paragraph with only one string? |
||
127 | if key == 'Para' and len(value) == 1 and value[0]['t'] == 'Str': |
||
128 | |||
129 | # Is it {tag}? |
||
130 | result = re.match('^{(?P<name>(?P<prefix>[a-zA-Z][\w.-]*)(?P<section>\:((?P<sharp>#(\.#)*)|(\d+(\.\d+)*)))?)}$', value[0]['c']) |
||
131 | if result: |
||
132 | |||
133 | prefix = result.group('prefix') |
||
134 | |||
135 | # Get the collection name |
||
136 | if result.group('sharp') == None: |
||
137 | name = result.group('name') |
||
138 | else: |
||
139 | level = (len(result.group('sharp')) - 1) // 2 + 1 |
||
140 | name = prefix + ':' + '.'.join(map(str, headers2[:level])) |
||
141 | |||
142 | # Is it an existing collection |
||
143 | if name in collections: |
||
144 | |||
145 | if format == 'latex': |
||
146 | # Special case for LaTeX output |
||
147 | if 'toccolor' in meta: |
||
148 | linkcolor = '\\hypersetup{linkcolor=' + stringify(meta['toccolor']['c'], format) + '}' |
||
149 | else: |
||
150 | linkcolor = '\\hypersetup{linkcolor=black}' |
||
151 | if result.group('sharp') == None: |
||
152 | suffix = '' |
||
153 | else: |
||
154 | suffix = '_' |
||
155 | return Para([RawInline('tex', linkcolor + '\\makeatletter\\@starttoc{' + name + suffix + '}\\makeatother')]) |
||
156 | |||
157 | else: |
||
158 | # Prepare the list |
||
159 | elements = [] |
||
160 | |||
161 | # Loop on the collection |
||
162 | for value in collections[name]: |
||
163 | |||
164 | # Add an item to the list |
||
165 | if pandocVersion() < '1.16': |
||
166 | # pandoc 1.15 |
||
167 | link = Link([Str(value['text'])], ['#' + prefix + ':' + value['identifier'], '']) |
||
168 | else: |
||
169 | # pandoc 1.16 |
||
170 | link = Link(['', [], []], [Str(value['text'])], ['#' + prefix + ':' + value['identifier'], '']) |
||
171 | |||
172 | elements.append([Plain([link])]) |
||
173 | |||
174 | # Return a bullet list |
||
175 | return BulletList(elements) |
||
176 | |||
177 | # Special case where the paragraph start with '{{...' |
||
178 | elif re.match('^{{[a-zA-Z][\w.-]*}$', value[0]['c']): |
||
179 | value[0]['c'] = value[0]['c'][1:] |
||
180 | |||
195 |