| 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 |
Parses and tokenizes HTML input, recognizing LilyPond in HTML. |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import unicode_literals |
| 25 |
|
| 26 |
from . import _token |
| 27 |
from . import Parser, FallthroughParser |
| 28 |
|
| 29 |
from . import lilypond |
| 30 |
|
| 31 |
|
| 32 |
class Comment(_token.Comment): |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
class CommentStart(Comment, _token.BlockCommentStart): |
| 37 |
rx = r"<!--" |
| 38 |
def update_state(self, state): |
| 39 |
state.enter(ParseComment()) |
| 40 |
|
| 41 |
|
| 42 |
class CommentEnd(Comment, _token.Leaver, _token.BlockCommentEnd): |
| 43 |
rx = r"-->" |
| 44 |
|
| 45 |
|
| 46 |
class String(_token.String): |
| 47 |
pass |
| 48 |
|
| 49 |
|
| 50 |
class Tag(_token.Token): |
| 51 |
pass |
| 52 |
|
| 53 |
|
| 54 |
class TagStart(Tag): |
| 55 |
rx = r"</?\w[-_:\w]*\b" |
| 56 |
def update_state(self, state): |
| 57 |
state.enter(ParseAttr()) |
| 58 |
|
| 59 |
|
| 60 |
class TagEnd(Tag, _token.Leaver): |
| 61 |
rx = r"/?>" |
| 62 |
|
| 63 |
|
| 64 |
class AttrName(_token.Token): |
| 65 |
rx = r"\w+([-_:]\w+)?" |
| 66 |
|
| 67 |
|
| 68 |
class EqualSign(_token.Token): |
| 69 |
rx = "=" |
| 70 |
def update_state(self, state): |
| 71 |
state.enter(ParseValue()) |
| 72 |
|
| 73 |
|
| 74 |
class Value(_token.Leaver): |
| 75 |
rx = r"\w+" |
| 76 |
|
| 77 |
|
| 78 |
class StringDQStart(String, _token.StringStart): |
| 79 |
rx = r'"' |
| 80 |
def update_state(self, state): |
| 81 |
state.enter(ParseStringDQ()) |
| 82 |
|
| 83 |
|
| 84 |
class StringSQStart(String, _token.StringStart): |
| 85 |
rx = r"'" |
| 86 |
def update_state(self, state): |
| 87 |
state.enter(ParseStringSQ()) |
| 88 |
|
| 89 |
|
| 90 |
class StringDQEnd(String, _token.StringEnd, _token.Leaver): |
| 91 |
rx = r'"' |
| 92 |
|
| 93 |
|
| 94 |
class StringSQEnd(String, _token.StringEnd, _token.Leaver): |
| 95 |
rx = r"'" |
| 96 |
|
| 97 |
|
| 98 |
class EntityRef(_token.Character): |
| 99 |
rx = r"\&(#\d+|#[xX][0-9A-Fa-f]+|[A-Za-z_:][\w.:_-]*);" |
| 100 |
|
| 101 |
|
| 102 |
class LilyPondTag(Tag): |
| 103 |
pass |
| 104 |
|
| 105 |
|
| 106 |
class LilyPondVersionTag(LilyPondTag): |
| 107 |
rx = r"<lilypondversion/?>" |
| 108 |
|
| 109 |
|
| 110 |
class LilyPondFileTag(LilyPondTag): |
| 111 |
rx = r"</?lilypondfile\b" |
| 112 |
def update_state(self, state): |
| 113 |
state.enter(ParseLilyPondFileOptions()) |
| 114 |
|
| 115 |
|
| 116 |
class LilyPondFileTagEnd(LilyPondTag, _token.Leaver): |
| 117 |
rx = r"/?>" |
| 118 |
|
| 119 |
|
| 120 |
class LilyPondInlineTag(LilyPondTag): |
| 121 |
rx = r"<lilypond\b" |
| 122 |
def update_state(self, state): |
| 123 |
state.enter(ParseLilyPondAttr()) |
| 124 |
|
| 125 |
|
| 126 |
class LilyPondCloseTag(LilyPondTag, _token.Leaver): |
| 127 |
rx = r"</lilypond>" |
| 128 |
|
| 129 |
|
| 130 |
class LilyPondTagEnd(LilyPondTag): |
| 131 |
rx = r">" |
| 132 |
def update_state(self, state): |
| 133 |
state.replace(ParseLilyPond()) |
| 134 |
|
| 135 |
|
| 136 |
class LilyPondInlineTagEnd(LilyPondTag, _token.Leaver): |
| 137 |
rx = r"/?>" |
| 138 |
|
| 139 |
|
| 140 |
class SemiColon(_token.Token): |
| 141 |
rx = r":" |
| 142 |
def update_state(self, state): |
| 143 |
state.replace(ParseLilyPondInline()) |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
# Parsers: |
| 148 |
|
| 149 |
class ParseHTML(Parser): |
| 150 |
mode = "html" |
| 151 |
items = ( |
| 152 |
_token.Space, |
| 153 |
LilyPondVersionTag, |
| 154 |
LilyPondFileTag, |
| 155 |
LilyPondInlineTag, |
| 156 |
CommentStart, |
| 157 |
TagStart, |
| 158 |
EntityRef, |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
class ParseAttr(Parser): |
| 163 |
items = ( |
| 164 |
_token.Space, |
| 165 |
TagEnd, |
| 166 |
AttrName, |
| 167 |
EqualSign, |
| 168 |
StringDQStart, |
| 169 |
StringSQStart, |
| 170 |
) |
| 171 |
|
| 172 |
|
| 173 |
class ParseStringDQ(Parser): |
| 174 |
default = String |
| 175 |
items = ( |
| 176 |
StringDQEnd, |
| 177 |
EntityRef, |
| 178 |
) |
| 179 |
|
| 180 |
|
| 181 |
class ParseStringSQ(Parser): |
| 182 |
default = String |
| 183 |
items = ( |
| 184 |
StringSQEnd, |
| 185 |
EntityRef, |
| 186 |
) |
| 187 |
|
| 188 |
|
| 189 |
class ParseComment(Parser): |
| 190 |
default = Comment |
| 191 |
items = ( |
| 192 |
CommentEnd, |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
class ParseValue(FallthroughParser): |
| 197 |
"""Finds a value or drops back.""" |
| 198 |
items = ( |
| 199 |
_token.Space, |
| 200 |
Value, |
| 201 |
) |
| 202 |
def fallthrough(self, state): |
| 203 |
state.leave() |
| 204 |
|
| 205 |
|
| 206 |
class ParseLilyPondAttr(Parser): |
| 207 |
items = ( |
| 208 |
_token.Space, |
| 209 |
AttrName, |
| 210 |
EqualSign, |
| 211 |
StringDQStart, |
| 212 |
StringSQStart, |
| 213 |
LilyPondTagEnd, |
| 214 |
SemiColon, |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
class ParseLilyPondFileOptions(Parser): |
| 219 |
items = ( |
| 220 |
_token.Space, |
| 221 |
AttrName, |
| 222 |
EqualSign, |
| 223 |
StringDQStart, |
| 224 |
StringSQStart, |
| 225 |
LilyPondFileTagEnd, |
| 226 |
) |
| 227 |
|
| 228 |
|
| 229 |
class ParseLilyPond(lilypond.ParseGlobal): |
| 230 |
items = ( |
| 231 |
LilyPondCloseTag, |
| 232 |
) + lilypond.ParseGlobal.items |
| 233 |
|
| 234 |
|
| 235 |
class ParseLilyPondInline(lilypond.ParseMusic): |
| 236 |
items = ( |
| 237 |
LilyPondInlineTagEnd, |
| 238 |
) + lilypond.ParseMusic.items |
| 239 |
|
| 240 |
|