tweaks and fixes, html mode has a few new features and is mostly working as desired

This commit is contained in:
Bill Zorn 2015-11-09 19:24:48 -08:00
parent ccb112021e
commit c9f8f251d5
6 changed files with 136 additions and 68 deletions

View file

@ -19,6 +19,12 @@ def main(fname, oname = None, verbose = True, encoding = 'std',
gatherer = False, for_forum = False, for_mse = False, gatherer = False, for_forum = False, for_mse = False,
creativity = False, vdump = False, for_html = False): creativity = False, vdump = False, for_html = False):
# there is a sane thing to do here (namely, produce both at the same time)
# but we don't support it yet.
if for_mse and for_html:
print 'ERROR - decode.py - incompatible formats "mse" and "html"'
return
fmt_ordered = cardlib.fmt_ordered_default fmt_ordered = cardlib.fmt_ordered_default
if encoding in ['std']: if encoding in ['std']:
@ -68,12 +74,12 @@ def main(fname, oname = None, verbose = True, encoding = 'std',
fstring += 'raw:\n' + card.raw + '\n' fstring += 'raw:\n' + card.raw + '\n'
fstring += '\n' fstring += '\n'
fstring += card.format(gatherer = gatherer, for_forum = for_forum, fstring += card.format(gatherer = gatherer, for_forum = for_forum,
vdump = vdump) vdump = vdump) + '\n'
fstring = fstring.replace('<', '(').replace('>', ')') fstring = fstring.replace('<', '(').replace('>', ')')
writer.write(('\n' + fstring[:-1]).replace('\n', '\n\t\t')) writer.write(('\n' + fstring[:-1]).replace('\n', '\n\t\t'))
else: else:
writer.write(card.format(gatherer = gatherer, for_forum = for_forum, writer.write((card.format(gatherer = gatherer, for_forum = for_forum,
vdump = vdump, for_html = for_html).encode('utf-8')) vdump = vdump, for_html = for_html) + '\n').encode('utf-8'))
if creativity: if creativity:
cstring = '~~ closest cards ~~\n' cstring = '~~ closest cards ~~\n'

View file

@ -555,7 +555,9 @@ class Card:
# the NN representation, use str() or format() for output intended for human # the NN representation, use str() or format() for output intended for human
# readers. # readers.
def encode(self, fmt_ordered = fmt_ordered_default, fmt_labeled = None, fieldsep = utils.fieldsep, randomize_fields = False, randomize_mana = False, initial_sep = True, final_sep = True): def encode(self, fmt_ordered = fmt_ordered_default, fmt_labeled = None,
fieldsep = utils.fieldsep, randomize_fields = False, randomize_mana = False,
initial_sep = True, final_sep = True):
outfields = [] outfields = []
for field in fmt_ordered: for field in fmt_ordered:
@ -602,19 +604,31 @@ class Card:
return outstr return outstr
def format(self, gatherer = False, for_forum = False, for_mse = False, vdump = False, for_html = False): def format(self, gatherer = False, for_forum = False, vdump = False, for_html = False):
linebreak = '\n'
if for_html:
linebreak = '<hr>' + linebreak
outstr = '' outstr = ''
if for_html:
outstr += '<div class="card-text">\n'
if gatherer: if gatherer:
cardname = titlecase(transforms.name_unpass_1_dashes(self.__dict__[field_name])) cardname = titlecase(transforms.name_unpass_1_dashes(self.__dict__[field_name]))
if vdump and not cardname: if vdump and not cardname:
cardname = '_NONAME_' cardname = '_NONAME_'
if for_forum: # in general, for_html overrides for_forum
if for_html:
outstr += '<b>'
elif for_forum:
outstr += '[b]' outstr += '[b]'
outstr += cardname outstr += cardname
if for_forum: if for_html:
outstr += '</b>'
elif for_forum:
outstr += '[/b]' outstr += '[/b]'
coststr = self.__dict__[field_cost].format(for_forum = for_forum) coststr = self.__dict__[field_cost].format(for_forum=for_forum, for_html=for_html)
if vdump or not coststr == '_NOCOST_': if vdump or not coststr == '_NOCOST_':
outstr += ' ' + coststr outstr += ' ' + coststr
@ -631,7 +645,7 @@ class Card:
if not self.valid: if not self.valid:
outstr += ' _INVALID_' outstr += ' _INVALID_'
outstr += '\n' outstr += linebreak
basetypes = map(str.capitalize, self.__dict__[field_types]) basetypes = map(str.capitalize, self.__dict__[field_types])
if vdump and len(basetypes) < 1: if vdump and len(basetypes) < 1:
@ -649,9 +663,9 @@ class Card:
if self.__dict__[field_loyalty]: if self.__dict__[field_loyalty]:
outstr += ' ((' + utils.from_unary(self.__dict__[field_loyalty]) + '))' outstr += ' ((' + utils.from_unary(self.__dict__[field_loyalty]) + '))'
outstr += '\n'
if self.__dict__[field_text].text: if self.__dict__[field_text].text:
outstr += linebreak
mtext = self.__dict__[field_text].text mtext = self.__dict__[field_text].text
mtext = transforms.text_unpass_1_choice(mtext, delimit = False) mtext = transforms.text_unpass_1_choice(mtext, delimit = False)
mtext = transforms.text_unpass_2_counters(mtext) mtext = transforms.text_unpass_2_counters(mtext)
@ -665,25 +679,36 @@ class Card:
newtext = Manatext('') newtext = Manatext('')
newtext.text = mtext newtext.text = mtext
newtext.costs = self.__dict__[field_text].costs newtext.costs = self.__dict__[field_text].costs
outstr += newtext.format(for_forum = for_forum)
outstr += '\n' outstr += newtext.format(for_forum = for_forum, for_html = for_html)
if vdump and self.__dict__[field_other]: if vdump and self.__dict__[field_other]:
if for_forum: outstr += linebreak
if for_html:
outstr += '<i>'
elif for_forum:
outstr += '[i]' outstr += '[i]'
else: else:
outstr += utils.dash_marker * 2 outstr += utils.dash_marker * 2
outstr += '\n'
for idx, value in self.__dict__[field_other]:
outstr += '<' + str(idx) + '> ' + str(value)
outstr += '\n'
if for_forum:
outstr = outstr[:-1] # hack off the last newline
outstr += '[/i]'
outstr += '\n'
elif for_html: first = True
for idx, value in self.__dict__[field_other]:
if for_html:
if not first:
outstr += '<br>\n'
else:
first = False
else:
outstr += linebreak
outstr += '(' + str(idx) + ') ' + str(value)
if for_html:
outstr += '</i>'
if for_forum:
outstr += '[/i]'
elif for_html and False:
outstr += '<div class="card-text">' outstr += '<div class="card-text">'
cardname = self.__dict__[field_name] cardname = self.__dict__[field_name]
#cardname = transforms.name_unpass_1_dashes(self.__dict__[field_name]) #cardname = transforms.name_unpass_1_dashes(self.__dict__[field_name])
@ -744,30 +769,33 @@ class Card:
if vdump and not cardname: if vdump and not cardname:
cardname = '_NONAME_' cardname = '_NONAME_'
outstr += cardname outstr += cardname
coststr = self.__dict__[field_cost].format(for_forum=for_forum, for_html=for_html)
if vdump or not coststr == '_NOCOST_':
outstr += ' ' + coststr
if vdump:
if not self.parsed:
outstr += ' _UNPARSED_'
if not self.valid:
outstr += ' _INVALID_'
outstr += linebreak
outstr += ' '.join(self.__dict__[field_supertypes] + self.__dict__[field_types])
if self.__dict__[field_subtypes]:
outstr += ' ' + utils.dash_marker + ' ' + ' '.join(self.__dict__[field_subtypes])
if self.__dict__[field_rarity]: if self.__dict__[field_rarity]:
if self.__dict__[field_rarity] in utils.json_rarity_unmap: if self.__dict__[field_rarity] in utils.json_rarity_unmap:
rarity = utils.json_rarity_unmap[self.__dict__[field_rarity]] rarity = utils.json_rarity_unmap[self.__dict__[field_rarity]]
else: else:
rarity = self.__dict__[field_rarity] rarity = self.__dict__[field_rarity]
outstr += ' (' + rarity.lower() + ')' outstr += ' (' + rarity.lower() + ')'
if vdump:
if not self.parsed:
outstr += ' _UNPARSED_'
if not self.valid:
outstr += ' _INVALID_'
outstr += '\n'
coststr = self.__dict__[field_cost].format(for_forum = for_forum)
if vdump or not coststr == '_NOCOST_':
outstr += coststr
outstr += '\n'
outstr += ' '.join(self.__dict__[field_supertypes] + self.__dict__[field_types])
if self.__dict__[field_subtypes]:
outstr += ' ' + utils.dash_marker + ' ' + ' '.join(self.__dict__[field_subtypes])
outstr += '\n'
if self.__dict__[field_text].text: if self.__dict__[field_text].text:
outstr += linebreak
mtext = self.__dict__[field_text].text mtext = self.__dict__[field_text].text
mtext = transforms.text_unpass_1_choice(mtext, delimit = True) mtext = transforms.text_unpass_1_choice(mtext, delimit = True)
#mtext = transforms.text_unpass_2_counters(mtext) #mtext = transforms.text_unpass_2_counters(mtext)
@ -780,31 +808,60 @@ class Card:
newtext = Manatext('') newtext = Manatext('')
newtext.text = mtext newtext.text = mtext
newtext.costs = self.__dict__[field_text].costs newtext.costs = self.__dict__[field_text].costs
outstr += newtext.format(for_forum = for_forum) + '\n'
outstr += newtext.format(for_forum=for_forum, for_html=for_html)
if self.__dict__[field_pt]: if self.__dict__[field_pt]:
outstr += linebreak
outstr += '(' + utils.from_unary(self.__dict__[field_pt]) + ')' outstr += '(' + utils.from_unary(self.__dict__[field_pt]) + ')'
outstr += '\n'
if self.__dict__[field_loyalty]: if self.__dict__[field_loyalty]:
outstr += linebreak
outstr += '((' + utils.from_unary(self.__dict__[field_loyalty]) + '))' outstr += '((' + utils.from_unary(self.__dict__[field_loyalty]) + '))'
outstr += '\n'
if vdump and self.__dict__[field_other]: if vdump and self.__dict__[field_other]:
outstr += linebreak
if for_html:
outstr += '<i>'
else:
outstr += utils.dash_marker * 2 outstr += utils.dash_marker * 2
outstr += '\n'
first = True
for idx, value in self.__dict__[field_other]: for idx, value in self.__dict__[field_other]:
outstr += '<' + str(idx) + '> ' + str(value) if for_html:
outstr += '\n' if not first:
outstr += '<br>\n'
else:
first = False
else:
outstr += linebreak
outstr += '(' + str(idx) + ') ' + str(value)
if for_html:
outstr += '</i>'
if self.bside: if self.bside:
if for_html: if for_html:
outstr += "<hr><hr>\n" outstr += '\n'
# force for_forum to false so that the inner div doesn't duplicate the forum
# spoiler of the bside
outstr += self.bside.format(gatherer=gatherer, for_forum=False, for_html=for_html, vdump=vdump)
else: else:
outstr += utils.dash_marker * 8 + '\n' outstr += linebreak
outstr += self.bside.format(gatherer = gatherer, for_forum = for_forum, for_html = for_html) outstr += utils.dash_marker * 8
outstr += linebreak
outstr += self.bside.format(gatherer=gatherer, for_forum=for_forum, for_html=for_html, vdump=vdump)
if for_html: if for_html:
if for_forum:
outstr += linebreak
# force for_html to false to create a copyable forum spoiler div
outstr += ('<div>'
+ self.format(gatherer=gatherer, for_forum=for_forum, for_html=False, vdump=vdump).replace('\n', '<br>')
+ '</div>')
outstr += "</div>" outstr += "</div>"
return outstr return outstr
def to_mse(self, print_raw = False, vdump = False): def to_mse(self, print_raw = False, vdump = False):

File diff suppressed because one or more lines are too long

View file

@ -137,9 +137,10 @@ def mtg_open_file(fname, verbose = False,
for card_src in text.split(utils.cardsep): for card_src in text.split(utils.cardsep):
if card_src: if card_src:
card = cardlib.Card(card_src, fmt_ordered=fmt_ordered) card = cardlib.Card(card_src, fmt_ordered=fmt_ordered)
# unlike opening from json, we still want to return invalid cards
cards += [card]
if card.valid: if card.valid:
valid += 1 valid += 1
cards += [card]
elif card.parsed: elif card.parsed:
invalid += 1 invalid += 1
else: else:

View file

@ -182,6 +182,8 @@ class Manatext:
text = self.text text = self.text
for cost in self.costs: for cost in self.costs:
text = text.replace(utils.reserved_mana_marker, cost.format(for_forum=for_forum, for_html=for_html), 1) text = text.replace(utils.reserved_mana_marker, cost.format(for_forum=for_forum, for_html=for_html), 1)
if for_html:
text = text.replace('\n', '<br>\n')
return text return text
def encode(self, randomize = False): def encode(self, randomize = False):

View file

@ -10,10 +10,9 @@ import config
mse_prepend = 'mse version: 0.3.8\ngame: magic\nstylesheet: m15\nset info:\n\tsymbol:\nstyling:\n\tmagic-m15:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay:\n\tmagic-m15-clear:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-extra-improved:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\tpt box symbols: magic-pt-symbols-extra.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-planeswalker:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-planeswalker-promo-black:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-promo-dka:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-token-clear:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker-4abil:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker-clear:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker-promo-black:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n' mse_prepend = 'mse version: 0.3.8\ngame: magic\nstylesheet: m15\nset info:\n\tsymbol:\nstyling:\n\tmagic-m15:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay:\n\tmagic-m15-clear:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-extra-improved:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\tpt box symbols: magic-pt-symbols-extra.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-planeswalker:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-planeswalker-promo-black:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-promo-dka:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-m15-token-clear:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker-4abil:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker-clear:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n\tmagic-new-planeswalker-promo-black:\n\t\ttext box mana symbols: magic-mana-small.mse-symbol-font\n\t\toverlay: \n'
# special chunk of text to start an HTML document. # special chunk of text to start an HTML document.
box_height = 350
import html_extra_data import html_extra_data
html_prepend = html_extra_data.html_prepend html_prepend = html_extra_data.html_prepend
html_append = "\n/body>\n</html>" html_append = "\n</body>\n</html>"
# encoding formats we know about # encoding formats we know about
formats = [ formats = [
@ -413,27 +412,21 @@ def mana_untranslate(manastr, for_forum = False, for_html = False):
sym = inner[idx:idx+symlen] sym = inner[idx:idx+symlen]
if sym in mana_symall_decode: if sym in mana_symall_decode:
idx += symlen idx += symlen
if for_forum:
jmanastr = jmanastr + mana_decode_direct_forum(sym)
if for_html: if for_html:
jmanastr = jmanastr + mana_decode_direct(sym) jmanastr = jmanastr + mana_decode_direct(sym)
jmanastr = jmanastr.replace(mana_open_delimiter, mana_html_open_delimiter) jmanastr = jmanastr.replace(mana_open_delimiter, mana_html_open_delimiter)
jmanastr = jmanastr.replace(mana_close_delimiter, mana_html_close_delimiter) jmanastr = jmanastr.replace(mana_close_delimiter, mana_html_close_delimiter)
jmanastr = jmanastr.replace(mana_open_delimiter, mana_html_open_delimiter) jmanastr = jmanastr.replace(mana_open_delimiter, mana_html_open_delimiter)
jmanastr = jmanastr.replace(mana_json_hybrid_delimiter, mana_html_hybrid_delimiter) jmanastr = jmanastr.replace(mana_json_hybrid_delimiter, mana_html_hybrid_delimiter)
elif for_forum:
jmanastr = jmanastr + mana_decode_direct_forum(sym)
else: else:
jmanastr = jmanastr + mana_decode_direct(sym) jmanastr = jmanastr + mana_decode_direct(sym)
break break
# otherwise we'll go into an infinite loop if we see a symbol we don't know # otherwise we'll go into an infinite loop if we see a symbol we don't know
if idx == old_idx: if idx == old_idx:
idx += 1 idx += 1
if for_forum:
if jmanastr == '':
return mana_forum_open_delimiter + str(colorless_total) + mana_forum_close_delimiter
else:
return (mana_forum_open_delimiter + ('' if colorless_total == 0
else str(colorless_total))
+ jmanastr + mana_forum_close_delimiter)
if for_html: if for_html:
if jmanastr == '': if jmanastr == '':
return mana_html_open_delimiter + str(colorless_total) + mana_html_close_delimiter return mana_html_open_delimiter + str(colorless_total) + mana_html_close_delimiter
@ -441,6 +434,14 @@ def mana_untranslate(manastr, for_forum = False, for_html = False):
return (mana_html_open_delimiter + ('' if colorless_total == 0 return (mana_html_open_delimiter + ('' if colorless_total == 0
else str(colorless_total)) else str(colorless_total))
+ mana_html_close_delimiter + jmanastr) + mana_html_close_delimiter + jmanastr)
elif for_forum:
if jmanastr == '':
return mana_forum_open_delimiter + str(colorless_total) + mana_forum_close_delimiter
else:
return (mana_forum_open_delimiter + ('' if colorless_total == 0
else str(colorless_total))
+ jmanastr + mana_forum_close_delimiter)
else: else:
if jmanastr == '': if jmanastr == '':
return mana_json_open_delimiter + str(colorless_total) + mana_json_close_delimiter return mana_json_open_delimiter + str(colorless_total) + mana_json_close_delimiter
@ -509,10 +510,10 @@ def from_symbols(s, for_forum = False, for_html = False):
# We have to do the right thing here, because the thing we replace exists in the thing # We have to do the right thing here, because the thing we replace exists in the thing
# we replace it with... # we replace it with...
for symstr in set(symstrs): for symstr in set(symstrs):
if for_forum: if for_html:
s = s.replace(symstr, symbol_forum_trans[symstr])
elif for_html:
s = s.replace(symstr, symbol_html_trans[symstr]) s = s.replace(symstr, symbol_html_trans[symstr])
elif for_forum:
s = s.replace(symstr, symbol_forum_trans[symstr])
else: else:
s = s.replace(symstr, symbol_trans[symstr]) s = s.replace(symstr, symbol_trans[symstr])
return s return s