Debian Codegraph @

ly/lex/_mode.py

raw
1 # This file is part of python-ly, https://pypi.python.org/pypi/python-ly
2 #
3 # Copyright (c) 2008 - 2015 by Wilbert Berendsen
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 # See http://www.gnu.org/licenses/ for more information.
19
20 """
21 Registry for the different modes used by the tokenizer.
22
23
24 There are two items in this module:
25
26 1. the modes dictionary.
27
28 This maps a mode name to a function returning the base parser class for
29 that mode. (This way the corresponding module only needs to be imported
30 when the mode is really needed.)
31
32 2. the guessMode function.
33
34 This tries to guess the type of the given text and returns a mode name.
35
36
37 You can easily add more modes in separate modules and mention them here,
38
39 Don't use this module directly! modes and guessMode are imported in the main
40 tokenize module.
41
42 """
43
44 from __future__ import unicode_literals
45
46 __all__ = ['modes', 'guessMode']
47
48
49 def _modes():
50 """Returns a dictionary mapping a mode name to a function.
51
52 The function should return the initial Parser instance for that mode.
53
54 """
55
56 def lilypond():
57 from . import lilypond
58 return lilypond.ParseGlobal
59
60 def scheme():
61 from . import scheme
62 return scheme.ParseScheme
63
64 def docbook():
65 from . import docbook
66 return docbook.ParseDocBook
67
68 def latex():
69 from . import latex
70 return latex.ParseLaTeX
71
72 def texinfo():
73 from . import texinfo
74 return texinfo.ParseTexinfo
75
76 def html():
77 from . import html
78 return html.ParseHTML
79
80 def mup():
81 from . import mup
82 return mup.ParseMup
83
84 # more modes can be added here
85 return locals()
86
87
88 # dictionary mapping mode name to a function returning initial parser instance
89 # for that mode. Can also be used to test the existence of a mode
90 modes = _modes()
91 del _modes
92
93
94 def guessMode(text):
95 """Tries to guess the type of the input text, using a quite fast heuristic.
96
97 Returns one of the strings also present as key in the modes dictionary.
98
99 """
100 text = text.lstrip()
101 if text.startswith(('%', '\\')):
102 if '\\version' in text or '\\relative' in text or '\\score' in text:
103 return "lilypond"
104 if "\\documentclass" in text or "\\begin{document}" in text:
105 return "latex"
106 return "lilypond"
107 if text.startswith("<<"):
108 return "lilypond"
109 if text.startswith("<"):
110 if 'DOCTYPE book' in text or "<programlisting" in text:
111 return "docbook"
112 else:
113 return "html"
114 if text.startswith(("#!", ";", "(")):
115 return "scheme"
116 if text.startswith('@'):
117 return "texinfo"
118 return "lilypond"
119
120
121
122 # dictionary mapping mode name to a default extension for a file of that mode.
123 extensions = {
124 'lilypond': '.ly',
125 'html': '.html',
126 'scheme': '.scm',
127 'latex': '.lytex',
128 'texinfo': '.texi',
129 'docbook': '.docbook',
130 'mup': '.mup',
131 }