diff --git a/decode.py b/decode.py index 4cde5fc..da95040 100755 --- a/decode.py +++ b/decode.py @@ -19,6 +19,12 @@ def main(fname, oname = None, verbose = True, encoding = 'std', gatherer = False, for_forum = False, for_mse = 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 if encoding in ['std']: @@ -68,12 +74,12 @@ def main(fname, oname = None, verbose = True, encoding = 'std', fstring += 'raw:\n' + card.raw + '\n' fstring += '\n' fstring += card.format(gatherer = gatherer, for_forum = for_forum, - vdump = vdump) + vdump = vdump) + '\n' fstring = fstring.replace('<', '(').replace('>', ')') writer.write(('\n' + fstring[:-1]).replace('\n', '\n\t\t')) else: - writer.write(card.format(gatherer = gatherer, for_forum = for_forum, - vdump = vdump, for_html = for_html).encode('utf-8')) + writer.write((card.format(gatherer = gatherer, for_forum = for_forum, + vdump = vdump, for_html = for_html) + '\n').encode('utf-8')) if creativity: cstring = '~~ closest cards ~~\n' diff --git a/lib/cardlib.py b/lib/cardlib.py index 6aaea9b..a12b856 100644 --- a/lib/cardlib.py +++ b/lib/cardlib.py @@ -555,7 +555,9 @@ class Card: # the NN representation, use str() or format() for output intended for human # 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 = [] for field in fmt_ordered: @@ -588,7 +590,7 @@ class Card: outfields = [''] + outfields if final_sep: outfields = outfields + [''] - + outstr = fieldsep.join(outfields) if self.bside: @@ -602,19 +604,31 @@ class Card: 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 = '
' + linebreak + outstr = '' + if for_html: + outstr += '
\n' + if gatherer: cardname = titlecase(transforms.name_unpass_1_dashes(self.__dict__[field_name])) if vdump and not cardname: cardname = '_NONAME_' - if for_forum: + # in general, for_html overrides for_forum + if for_html: + outstr += '' + elif for_forum: outstr += '[b]' outstr += cardname - if for_forum: + if for_html: + outstr += '' + elif for_forum: 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_': outstr += ' ' + coststr @@ -631,7 +645,7 @@ class Card: if not self.valid: outstr += ' _INVALID_' - outstr += '\n' + outstr += linebreak basetypes = map(str.capitalize, self.__dict__[field_types]) if vdump and len(basetypes) < 1: @@ -649,9 +663,9 @@ class Card: if self.__dict__[field_loyalty]: outstr += ' ((' + utils.from_unary(self.__dict__[field_loyalty]) + '))' - outstr += '\n' - if self.__dict__[field_text].text: + outstr += linebreak + mtext = self.__dict__[field_text].text mtext = transforms.text_unpass_1_choice(mtext, delimit = False) mtext = transforms.text_unpass_2_counters(mtext) @@ -665,25 +679,36 @@ class Card: newtext = Manatext('') newtext.text = mtext 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 for_forum: + outstr += linebreak + + if for_html: + outstr += '' + elif for_forum: outstr += '[i]' else: 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 += '
\n' + else: + first = False + else: + outstr += linebreak + outstr += '(' + str(idx) + ') ' + str(value) + + if for_html: + outstr += '
' + if for_forum: + outstr += '[/i]' + + elif for_html and False: outstr += '
' cardname = 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: cardname = '_NONAME_' 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] in utils.json_rarity_unmap: rarity = utils.json_rarity_unmap[self.__dict__[field_rarity]] else: rarity = self.__dict__[field_rarity] 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: + outstr += linebreak + mtext = self.__dict__[field_text].text mtext = transforms.text_unpass_1_choice(mtext, delimit = True) #mtext = transforms.text_unpass_2_counters(mtext) @@ -780,31 +808,60 @@ class Card: newtext = Manatext('') newtext.text = mtext 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]: + outstr += linebreak outstr += '(' + utils.from_unary(self.__dict__[field_pt]) + ')' - outstr += '\n' if self.__dict__[field_loyalty]: + outstr += linebreak outstr += '((' + utils.from_unary(self.__dict__[field_loyalty]) + '))' - outstr += '\n' if vdump and self.__dict__[field_other]: - outstr += utils.dash_marker * 2 - outstr += '\n' + outstr += linebreak + + if for_html: + outstr += '' + else: + outstr += utils.dash_marker * 2 + + first = True for idx, value in self.__dict__[field_other]: - outstr += '<' + str(idx) + '> ' + str(value) - outstr += '\n' + if for_html: + if not first: + outstr += '
\n' + else: + first = False + else: + outstr += linebreak + outstr += '(' + str(idx) + ') ' + str(value) + + if for_html: + outstr += '
' if self.bside: if for_html: - outstr += "

\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: - outstr += utils.dash_marker * 8 + '\n' - outstr += self.bside.format(gatherer = gatherer, for_forum = for_forum, for_html = for_html) + outstr += linebreak + 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_forum: + outstr += linebreak + # force for_html to false to create a copyable forum spoiler div + outstr += ('
' + + self.format(gatherer=gatherer, for_forum=for_forum, for_html=False, vdump=vdump).replace('\n', '
') + + '
') outstr += "
" + return outstr def to_mse(self, print_raw = False, vdump = False): diff --git a/lib/html_extra_data.py b/lib/html_extra_data.py index e3cd6ab..0d7c3ac 100644 --- a/lib/html_extra_data.py +++ b/lib/html_extra_data.py @@ -1 +1,2 @@ -html_prepend = "\n\n\n" +box_width = 350 +html_prepend = "\n\n\n" diff --git a/lib/jdecode.py b/lib/jdecode.py index fa2d3f7..0c3351a 100644 --- a/lib/jdecode.py +++ b/lib/jdecode.py @@ -137,9 +137,10 @@ def mtg_open_file(fname, verbose = False, for card_src in text.split(utils.cardsep): if card_src: 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: valid += 1 - cards += [card] elif card.parsed: invalid += 1 else: diff --git a/lib/manalib.py b/lib/manalib.py index 7875b33..dbbf31a 100644 --- a/lib/manalib.py +++ b/lib/manalib.py @@ -181,7 +181,9 @@ class Manatext: def format(self, for_forum = False, for_html = False): text = self.text 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', '
\n') return text def encode(self, randomize = False): diff --git a/lib/utils.py b/lib/utils.py index 7904c5c..6b514a5 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -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' # special chunk of text to start an HTML document. -box_height = 350 import html_extra_data html_prepend = html_extra_data.html_prepend -html_append = "\n/body>\n" +html_append = "\n\n" # encoding formats we know about formats = [ @@ -413,27 +412,21 @@ def mana_untranslate(manastr, for_forum = False, for_html = False): sym = inner[idx:idx+symlen] if sym in mana_symall_decode: idx += symlen - if for_forum: - jmanastr = jmanastr + mana_decode_direct_forum(sym) if for_html: jmanastr = jmanastr + mana_decode_direct(sym) jmanastr = jmanastr.replace(mana_open_delimiter, mana_html_open_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_json_hybrid_delimiter, mana_html_hybrid_delimiter) + elif for_forum: + jmanastr = jmanastr + mana_decode_direct_forum(sym) else: jmanastr = jmanastr + mana_decode_direct(sym) break # otherwise we'll go into an infinite loop if we see a symbol we don't know if idx == old_idx: 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 jmanastr == '': 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 else str(colorless_total)) + 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: if jmanastr == '': 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 replace it with... for symstr in set(symstrs): - if for_forum: - s = s.replace(symstr, symbol_forum_trans[symstr]) - elif for_html: + if for_html: s = s.replace(symstr, symbol_html_trans[symstr]) + elif for_forum: + s = s.replace(symstr, symbol_forum_trans[symstr]) else: s = s.replace(symstr, symbol_trans[symstr]) return s