def convert_to_hrecipe(i_file, o_file): """Uses pybeerxml to read the import file, then coughs out an appropriate HTML file, marked up with h-recipe. The input (and therefore output) file may contain more than one recipe. """ log_it('INFO: Converting %s to %s' % (i_file, o_file), 1) parser = pybeerxml.Parser() recipes = parser.parse(i_file) with open(o_file, mode='w') as output_file: output_file.write('<!doctype html>\n<html>\n<head>\n<title>Conversion results for %s</title>\n' % i_file) output_file.write('<meta charset="utf-8" />\n</head>\n<body>\n\n' ) output_file.write('<h1>Conversion results for %s</h1>\n\n' % i_file) output_file.write("""<p>This file was generated by <code><a rel="muse" href="https://github.com/patrick-brian-mooney/beerxml-to-hrecipe">Patrick Mooney's beerxml-to-hrecipe</a></code>.</p>""") log_it('INFO: HTML header written', 3) log_it('INFO: there are %d recipes' % len(recipes), 2) for r in recipes: # Next, ingredients output_file.write('\n\n<h2 id="ingredients">Ingredients</h2>\n\n<ul class="h-recipe">\n') log_it('INFO: there are %d fermentables' % len(r.fermentables), 2) for f in r.fermentables: log_it('INFO: creating line for fermentable %s' % f.name, 3) output_file.write(' <li class="p-ingredient">%.2f lb. %s</li>\n' % (2.20462 * f.amount, f.name)) log_it('INFO: there are %d hops' % len(r.hops), 2) for h in r.hops: log_it('INFO: creating line for hop %s' % h.name, 3) output_file.write(' <li class="p-ingredient">%.2f oz. %s hops (%s%% A.A.) at %.0f min.</li>\n' % (2.20462 * 16 * h.amount, h.name, h.alpha, h.time)) log_it('INFO: there are %d yeasts' % len(r.yeasts), 2) for y in r.yeasts: log_it('INFO: creating line for yeast %s' % y.name, 3) output_file.write(' <li class="p-ingredient">%s %s</li>\n' % (y.laboratory, y.name)) output_file.write('</ul>') output_file.write('\n\n<h2 id="procedure">Procedure</h2>\n') output_file.write('<div class="e-instructions">\n<p>Write-up goes here.</p>\n</div>\n') output_file.write('\n<p class="vcalendar"><span class="vevent"><strong class="summary description">Brew day</strong>: <abbr title="" class="dtstart">date</abbr></span><br />\n') output_file.write('<strong>Predicted original gravity:</strong> %.4f<br />\n' % r.og) output_file.write('<strong>Measured original gravity:</strong> <br />\n') output_file.write('<strong>Estimated IBUs:</strong> %.1f<br />\n' % r.ibu) output_file.write('<strong>Predicted final gravity</strong> %.4f<br />\n' % r.fg) output_file.write('<strong>Predicted ABV:</strong> %.1f<br />\n' % r.abv) output_file.write('\n<span class="vevent"><strong class="summary description">Bottling day </strong>: <abbr title="" class="dtstart">date</abbr></span><br />\n') output_file.write('<strong>Final gravity:</strong> <br />\n') output_file.write('<strong>Estimated ABV:</strong></p>\n') output_file.write('\n<p><strong>Yield</strong>:</p>\n') output_file.write('<ul>\n<li></li>\n</ul>\n') output_file.write('<p><strong>Total yield:</strong></p>\n') log_it('INFO: recipe summary information written for recipe "%s"' % r.name, 3) log_it('<h2 id="observations">Observations</h2>\n') log_it('<ul class="vcalendar xoxo">\n<li></li>\n</ul>') output_file.write("\n</body>\n</html>") log_it('Output file written!')
def load_file(path, units={}): """Parse BeerXML file. `path` is the path to a BeerXML file. `units` is a dictionary defining the units used. `units` will be unpacked and used during the creation of a `MarkdownRecipe` object. Return a list of MarkdownRecipe objects. If an exception is raised during parsing, the message is printed to stderr and an empty list is returned. """ try: result = pybeerxml.Parser().parse(path) except Exception as err: print('Error parsing {}: {}'.format(path, err), file=sys.stderr) return [] recipes = [] for recipe in result: recipes.append(MarkdownRecipe(recipe, **units)) return recipes
def md_coffee_stout(): """Return the sample coffee stout recipe as a MarkdownRecipe object.""" return MarkdownRecipe( pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0])
def md_weizen(): """Return the sample weizen recipe as a MarkdownRecipe object.""" return MarkdownRecipe( pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0])
def md_recipes(): """Generate a list of MarkdownRecipe objects.""" coffeestout = pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0] weizen = pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0] return [MarkdownRecipe(coffeestout), MarkdownRecipe(weizen)]
def xml_recipes(): """Generate a list of pybeerxml.Recipe objects.""" coffeestout = pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0] weizen = pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0] return [coffeestout, weizen]
if __name__ == "__main__": argparser = argparse.ArgumentParser() argparser.add_argument("inputfile", help="The beerXML file to use as input") argparser.add_argument("outputfile", help="The pdf file to write to") argparser.add_argument("-t", "--template", required=True, help="The jinja2 template to use") argparser.add_argument("--boiltime", type=int, help="Override boil-time with this") args = argparser.parse_args() parser = pybeerxml.Parser() recipes = parser.parse(args.inputfile) for recipe in recipes: if args.boiltime: recipe.boil_time = args.boiltime notes = [] if recipe.notes: for noteline in recipe.notes.split('\n'): notes.append(noteline.strip()) template = jinja2.Template(open(args.template, 'r').read()) output = template.render(recipe=recipe, notes=notes) of = tempfile.NamedTemporaryFile(mode='w') of.write(output)