| Conditions | 32 | 
| Total Lines | 139 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| 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 getDefined() 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 | ||
| 119 | # Is the icon correct? | ||
| 120 | try: | ||
| 121 | if name in doc.getIconFont.css_icons: | ||
| 122 |                     icons.append({'name': name, 'color': lowerColor}) | ||
| 123 | else: | ||
| 124 |                     debug('[WARNING] pandoc-latex-tip: ' + name + ' is not a correct icon name') | ||
| 125 | except FileNotFoundError: | ||
| 126 |                 debug('[WARNING] pandoc-latex-tip: error in accessing to icons definition') | ||
| 127 | |||
| 128 | return icons | ||
| 129 | |||
| 130 | def get_prefix(definition): | ||
| 131 | if 'position' in definition: | ||
| 132 | if definition['position'] == 'right': | ||
| 133 | return '\\normalmarginpar' | ||
| 134 | elif definition['position'] == 'left': | ||
| 135 | return '\\reversemarginpar' | ||
| 136 | else: | ||
| 137 |             debug('[WARNING] pandoc-latex-tip: ' + position + ' is not a correct position; using left') | ||
| 138 | return '\\reversemarginpar' | ||
| 139 | return '\\reversemarginpar' | ||
| 140 | |||
| 141 | def get_size(definition): | ||
| 142 | # Get the size | ||
| 143 | size = '18' | ||
| 144 | if 'size' in definition: | ||
| 145 | try: | ||
| 146 | intValue = int(definition['size']) | ||
| 147 | if intValue > 0: | ||
| 148 | size = str(intValue) | ||
| 149 | else: | ||
| 150 |                 debug('[WARNING] pandoc-latex-tip: size must be greater than 0; using ' + size) | ||
| 151 | except ValueError: | ||
| 152 |             debug('[WARNING] pandoc-latex-tip: size must be a number; using ' + size) | ||
| 153 | return size | ||
| 154 | |||
| 155 | def get_images(icons, size): | ||
| 156 | # Generate the LaTeX image code | ||
| 157 | images = [] | ||
| 158 | |||
| 159 | for icon in icons: | ||
| 160 | |||
| 161 | # Get the apps dirs | ||
| 162 | from pkg_resources import get_distribution | ||
| 163 | from appdirs import AppDirs | ||
| 164 |         dirs = AppDirs('pandoc_latex_tip', version = get_distribution('pandoc_latex_tip').version) | ||
| 165 | |||
| 166 | # Get the image from the App cache folder | ||
| 167 | image = dirs.user_cache_dir + '/' + icon['color'] + '/' + icon['name'] + '.png' | ||
| 168 | |||
| 169 | # Create the image if not existing in the cache | ||
| 170 | try: | ||
| 171 | if not os.path.isfile(image): | ||
| 172 | |||
| 173 | # Create the image in the cache | ||
| 174 | doc.getIconFont.export_icon( | ||
| 175 | icon['name'], | ||
| 176 | size = 512, | ||
| 177 | color = icon['color'], | ||
| 178 | export_dir = dirs.user_cache_dir + '/' + icon['color'] | ||
| 179 | ) | ||
| 180 | |||
| 181 | # Add the LaTeX image | ||
| 182 |             images.append('\\includegraphics[width=' + size + 'pt]{' + image + '}') | ||
| 183 | except FileNotFoundError: | ||
| 184 |             debug('[WARNING] pandoc-latex-tip: error in generating image') | ||
| 185 | |||
| 186 | return images | ||
| 187 | |||
| 188 | def add_definition(doc, definition): | ||
| 189 | # Get the classes | ||
| 190 | classes = definition['classes'] | ||
| 191 | |||
| 192 | # Get the icons | ||
| 193 | icons = get_icons(doc, definition) | ||
| 194 | |||
| 195 | # Add a definition if correct | ||
| 196 | if bool(classes) and bool(icons): | ||
| 197 | |||
| 198 | # Get the images | ||
| 199 | images = get_images(icons, get_size(definition)) | ||
| 200 | |||
| 201 | # Get the prefix | ||
| 202 | prefix = get_prefix(definition) | ||
| 203 | |||
| 204 |         doc.defined.append({'classes' : set(classes), 'latex': latex_code(prefix, images)}) | ||
| 205 | |||
| 206 | def main(doc = None): | ||
| 207 | run_filter(tip, prepare = prepare, finalize = finalize, doc = doc) | ||
| 208 | |||
| 209 | if __name__ == '__main__': | ||
| 210 | main() | ||
| 211 | |||
| 212 |