diff --git a/decode.py b/decode.py index 6f53da0..4cde5fc 100755 --- a/decode.py +++ b/decode.py @@ -17,7 +17,7 @@ def exclude_sets(cardset): def main(fname, oname = None, verbose = True, encoding = 'std', gatherer = False, for_forum = False, for_mse = False, - creativity = False, vdump = False): + creativity = False, vdump = False, for_html = False): fmt_ordered = cardlib.fmt_ordered_default @@ -53,6 +53,11 @@ def main(fname, oname = None, verbose = True, encoding = 'std', if for_mse: # have to prepend a massive chunk of formatting info writer.write(utils.mse_prepend) + + if for_html: + # have to preapend html info + writer.write(utils.html_prepend) + for card in cards: if for_mse: writer.write(card.to_mse().encode('utf-8')) @@ -68,7 +73,7 @@ def main(fname, oname = None, verbose = True, encoding = 'std', writer.write(('\n' + fstring[:-1]).replace('\n', '\n\t\t')) else: writer.write(card.format(gatherer = gatherer, for_forum = for_forum, - vdump = vdump).encode('utf-8')) + vdump = vdump, for_html = for_html).encode('utf-8')) if creativity: cstring = '~~ closest cards ~~\n' @@ -95,8 +100,15 @@ def main(fname, oname = None, verbose = True, encoding = 'std', if for_mse: # more formatting info writer.write('version control:\n\ttype: none\napprentice code: ') + if for_html: + # closing the html file + writer.write(utils.html_append) if oname: + if for_html: + print oname + # if ('.html' != oname[-]) + # oname += '.html' if verbose: print 'Writing output to: ' + oname with open(oname, 'w') as ofile: @@ -144,10 +156,14 @@ if __name__ == '__main__': help='dump out lots of information about invalid cards') parser.add_argument('-v', '--verbose', action='store_true', help='verbose output') - parser.add_argument('-mse', '--mse', action='store_true', help='use Magic Set Editor 2 encoding; will output as .mse-set file') - + parser.add_argument('-mse', '--mse', action='store_true', + help='use Magic Set Editor 2 encoding; will output as .mse-set file') + parser.add_argument('-html', '--html', action='store_true', help='create a .html file with pretty forum formatting') + args = parser.parse_args() + main(args.infile, args.outfile, verbose = args.verbose, encoding = args.encoding, gatherer = args.gatherer, for_forum = args.forum, for_mse = args.mse, - creativity = args.creativity, vdump = args.dump) + creativity = args.creativity, vdump = args.dump, for_html = args.html) + exit(0) diff --git a/lib/cardlib.py b/lib/cardlib.py index bb23cc4..6aaea9b 100644 --- a/lib/cardlib.py +++ b/lib/cardlib.py @@ -392,6 +392,7 @@ class Card: def __init__(self, src, fmt_ordered = fmt_ordered_default, fmt_labeled = fmt_labeled_default, fieldsep = utils.fieldsep, linetrans = True): + # source fields, exactly one will be set self.json = None self.raw = None @@ -554,10 +555,7 @@ 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: @@ -604,7 +602,7 @@ class Card: return outstr - def format(self, gatherer = False, for_forum = False, for_mse = False, vdump = False): + def format(self, gatherer = False, for_forum = False, for_mse = False, vdump = False, for_html = False): outstr = '' if gatherer: cardname = titlecase(transforms.name_unpass_1_dashes(self.__dict__[field_name])) @@ -659,7 +657,7 @@ class Card: mtext = transforms.text_unpass_2_counters(mtext) #mtext = transforms.text_unpass_3_uncast(mtext) mtext = transforms.text_unpass_4_unary(mtext) - mtext = transforms.text_unpass_5_symbols(mtext, for_forum) + mtext = transforms.text_unpass_5_symbols(mtext, for_forum, for_html) mtext = sentencecase(mtext) mtext = transforms.text_unpass_6_cardname(mtext, cardname) mtext = transforms.text_unpass_7_newlines(mtext) @@ -685,6 +683,61 @@ class Card: outstr += '[/i]' outstr += '\n' + elif for_html: + outstr += '
' + cardname = self.__dict__[field_name] + #cardname = transforms.name_unpass_1_dashes(self.__dict__[field_name]) + if vdump and not cardname: + cardname = '_NONAME_' + outstr += cardname + ' ' + + coststr = self.__dict__[field_cost].format(for_html = for_html) + if vdump or not coststr == '_NOCOST_': + outstr += coststr + outstr += '
' + + 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() + ') ' + 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: + mtext = self.__dict__[field_text].text + mtext = transforms.text_unpass_1_choice(mtext, delimit = True) + #mtext = transforms.text_unpass_2_counters(mtext) + #mtext = transforms.text_unpass_3_uncast(mtext) + mtext = transforms.text_unpass_4_unary(mtext) + mtext = transforms.text_unpass_5_symbols(mtext,for_forum, for_html) + #mtext = transforms.text_unpass_6_cardname(mtext, cardname) + mtext = transforms.text_unpass_7_newlines(mtext).replace("\n", "
") + #mtext = transforms.text_unpass_8_unicode(mtext) + newtext = Manatext('') + newtext.text = mtext + newtext.costs = self.__dict__[field_text].costs + outstr += newtext.format(for_html = for_html) + '\n' + + if self.__dict__[field_pt]: + outstr += '
(' + utils.from_unary(self.__dict__[field_pt]) + ')
' + outstr += '\n' + + if self.__dict__[field_loyalty]: + outstr += '((' + utils.from_unary(self.__dict__[field_loyalty]) + '))' + outstr += '\n' + + if vdump and self.__dict__[field_other]: + outstr += utils.dash_marker * 2 + outstr += '\n' + for idx, value in self.__dict__[field_other]: + outstr += '<' + str(idx) + '> ' + str(value) + outstr += '\n' else: cardname = self.__dict__[field_name] #cardname = transforms.name_unpass_1_dashes(self.__dict__[field_name]) @@ -720,7 +773,7 @@ class Card: #mtext = transforms.text_unpass_2_counters(mtext) #mtext = transforms.text_unpass_3_uncast(mtext) mtext = transforms.text_unpass_4_unary(mtext) - mtext = transforms.text_unpass_5_symbols(mtext, for_forum) + mtext = transforms.text_unpass_5_symbols(mtext, for_forum, for_html) #mtext = transforms.text_unpass_6_cardname(mtext, cardname) mtext = transforms.text_unpass_7_newlines(mtext) #mtext = transforms.text_unpass_8_unicode(mtext) @@ -745,9 +798,13 @@ class Card: outstr += '\n' if self.bside: - outstr += utils.dash_marker * 8 + '\n' - outstr += self.bside.format(gatherer = gatherer, for_forum = for_forum) - + if for_html: + outstr += "

\n" + else: + outstr += utils.dash_marker * 8 + '\n' + outstr += self.bside.format(gatherer = gatherer, for_forum = for_forum, for_html = for_html) + if for_html: + outstr += "
" return outstr def to_mse(self, print_raw = False, vdump = False): @@ -788,7 +845,7 @@ class Card: mtext = transforms.text_unpass_2_counters(mtext) mtext = transforms.text_unpass_3_uncast(mtext) mtext = transforms.text_unpass_4_unary(mtext) - mtext = transforms.text_unpass_5_symbols(mtext, False) + mtext = transforms.text_unpass_5_symbols(mtext, False, False) mtext = sentencecase(mtext) # I don't really want these MSE specific passes in transforms, # but they could be pulled out separately somewhere else in here. @@ -855,7 +912,7 @@ class Card: mtext2 = transforms.text_unpass_2_counters(mtext2) mtext2 = transforms.text_unpass_3_uncast(mtext2) mtext2 = transforms.text_unpass_4_unary(mtext2) - mtext2 = transforms.text_unpass_5_symbols(mtext2, False) + mtext2 = transforms.text_unpass_5_symbols(mtext2, False, False) mtext2 = sentencecase(mtext2) mtext2 = mtext2.replace(utils.this_marker, '' + utils.this_marker + '') diff --git a/lib/html_extra_data.py b/lib/html_extra_data.py new file mode 100644 index 0000000..e3cd6ab --- /dev/null +++ b/lib/html_extra_data.py @@ -0,0 +1 @@ +html_prepend = "\n\n\n" diff --git a/lib/manalib.py b/lib/manalib.py index 688f3f2..7875b33 100644 --- a/lib/manalib.py +++ b/lib/manalib.py @@ -108,12 +108,13 @@ class Manacost: return utils.mana_untranslate(utils.mana_open_delimiter + ''.join(self.sequence) + utils.mana_close_delimiter) - def format(self, for_forum = False): + def format(self, for_forum = False, for_html = False): if self.none: return '_NOCOST_' + else: return utils.mana_untranslate(utils.mana_open_delimiter + ''.join(self.sequence) - + utils.mana_close_delimiter, for_forum) + + utils.mana_close_delimiter, for_forum, for_html) def encode(self, randomize = False): if self.none: @@ -177,10 +178,10 @@ class Manatext: text = text.replace(utils.reserved_mana_marker, str(cost), 1) return text - def format(self, for_forum = False): + 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), 1) + text = text.replace(utils.reserved_mana_marker, cost.format(for_forum = for_forum, for_html = for_html), 1) return text def encode(self, randomize = False): diff --git a/lib/transforms.py b/lib/transforms.py index 4bbfb68..319955c 100644 --- a/lib/transforms.py +++ b/lib/transforms.py @@ -515,8 +515,8 @@ def text_unpass_4_unary(s): return utils.from_unary(s) -def text_unpass_5_symbols(s, for_forum): - return utils.from_symbols(s, for_forum = for_forum) +def text_unpass_5_symbols(s, for_forum, for_html): + return utils.from_symbols(s, for_forum = for_forum, for_html = for_html) def text_unpass_6_cardname(s, name): diff --git a/lib/utils.py b/lib/utils.py index 9332142..7904c5c 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -9,6 +9,12 @@ import config # special chunk of text that Magic Set Editor 2 requires at the start of all set files. 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" + # encoding formats we know about formats = [ 'std', @@ -136,6 +142,9 @@ mana_json_close_delimiter = mana_close_delimiter mana_json_hybrid_delimiter = '/' mana_forum_open_delimiter = '[mana]' mana_forum_close_delimiter = '[/mana]' +mana_html_open_delimiter = "" +mana_html_hybrid_delimiter = '-' mana_unary_marker = '' # if the same as unary_marker, from_unary WILL replace numbers in mana costs mana_unary_counter = unary_counter @@ -386,7 +395,7 @@ def mana_translate(jmanastr): # convert an encoded mana string back to json mana_symlen_min = min([len(sym) for sym in mana_symall_decode]) mana_symlen_max = max([len(sym) for sym in mana_symall_decode]) -def mana_untranslate(manastr, for_forum = False): +def mana_untranslate(manastr, for_forum = False, for_html = False): inner = manastr[1:-1] jmanastr = '' colorless_total = 0 @@ -406,6 +415,12 @@ def mana_untranslate(manastr, for_forum = False): 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) else: jmanastr = jmanastr + mana_decode_direct(sym) break @@ -419,6 +434,13 @@ def mana_untranslate(manastr, for_forum = False): 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 + else: + return (mana_html_open_delimiter + ('' if colorless_total == 0 + else str(colorless_total)) + + mana_html_close_delimiter + jmanastr) else: if jmanastr == '': return mana_json_open_delimiter + str(colorless_total) + mana_json_close_delimiter @@ -464,6 +486,11 @@ symbol_forum_trans = { tap_marker : mana_forum_open_delimiter + json_symbol_tap + mana_forum_close_delimiter, untap_marker : mana_forum_open_delimiter + json_symbol_untap + mana_forum_close_delimiter, } +symbol_html_trans = { + tap_marker : mana_html_open_delimiter + json_symbol_tap + mana_html_close_delimiter, + untap_marker : mana_html_open_delimiter + json_symbol_untap + mana_html_close_delimiter, +} + json_symbol_regex = (re.escape(mana_json_open_delimiter) + '[' + json_symbol_tap + json_symbol_tap.lower() + json_symbol_untap + json_symbol_untap.lower() @@ -476,7 +503,7 @@ def to_symbols(s): s = s.replace(jsymstr, json_symbol_trans[jsymstr]) return s -def from_symbols(s, for_forum = False): +def from_symbols(s, for_forum = False, for_html = False): symstrs = re.findall(symbol_regex, s) #for symstr in sorted(symstrs, lambda x,y: cmp(len(x), len(y)), reverse = True): # We have to do the right thing here, because the thing we replace exists in the thing @@ -484,6 +511,8 @@ def from_symbols(s, for_forum = False): for symstr in set(symstrs): if for_forum: s = s.replace(symstr, symbol_forum_trans[symstr]) + elif for_html: + s = s.replace(symstr, symbol_html_trans[symstr]) else: s = s.replace(symstr, symbol_trans[symstr]) return s diff --git a/sortcards.py b/sortcards.py index df3431e..0c688b1 100644 --- a/sortcards.py +++ b/sortcards.py @@ -91,10 +91,10 @@ def sortcards(cards): # color classes need to find the mana cost fields = card.split('|') - if len(fields) != 10: + if len(fields) != 11: classes['unknown color'] += [card] else: - cost = fields[7] + cost = fields[8] color_count = 0 if 'W' in cost or 'U' in cost or 'B' in cost or 'R' in cost or 'G' in cost: if 'W' in cost: