diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7488200 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +composer.lock +vendor/ +output/* +invoices/*/* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..22243a0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (C) 2012 Tyler King + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6300944 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Markdown Invoice + +This is a simple Symfony console application that I use to turn Markdown invoices into HTML or PDF. + +## Useage + +```bash +bin/markdown-invoice generate --help +Usage: + generate [--output[="..."]] src [invoice] + +Arguments: + src Location of invoice; ex: "paid", "unpaid" + invoice What invoice to generate for? + +Options: + --output Output to format; html or pdf (default: "html") + --help (-h) Display this help message. + --quiet (-q) Do not output any message. + --verbose (-v) Increase verbosity of messages. + --version (-V) Display this application version. + --ansi Force ANSI output. + --no-ansi Disable ANSI output. + --no-interaction (-n) Do not ask any interactive question. +``` + +## Requirements + +- [PHP](http://php.net) 5.4.x \ No newline at end of file diff --git a/bin/markdown-invoice b/bin/markdown-invoice new file mode 100755 index 0000000..9769271 --- /dev/null +++ b/bin/markdown-invoice @@ -0,0 +1,116 @@ +#!/usr/bin/env php +register('generate') + ->addArgument( + 'src', + InputArgument::REQUIRED, + 'Location of invoice; ex: "paid", "unpaid"' + ) + ->addArgument( + 'invoice', + InputArgument::OPTIONAL, + 'What invoice to generate for?' + ) + ->addOption( + 'output', + null, + InputOption::VALUE_OPTIONAL, + 'Output to format; html or pdf', + 'html' + ) + ->setDescription('Generates HTML versions of a Markdown Invoice.') + ->setCode(function(InputInterface $input, OutputInterface $output) use($console) { + $output_format = $input->getOption('output'); + $invoice = $input->getArgument('invoice'); + $invoice_src = $input->getArgument('src'); + + $invoice_dir = __DIR__."/../invoices/{$invoice_src}/"; + $output_dir = __DIR__."/../output/"; + $layout_path = __DIR__.'/../invoices/layout.html'; + + if (null === $invoice) { + $output->writeln("Please choose an invoice in \"{$invoice_src}\"..."); + $output->writeln(''); + + $i = 1; + $files = []; + foreach (new DirectoryIterator($invoice_dir) as $file) { + if (in_array($file->getExtension(), ['md', 'markdown'])) { + $files[$i] = str_replace(['.md', '.markdown'], '', $file->getBasename()); + $output->writeln("{$i}. ".$file->getBasename()); + $i++; + } + } + + $dialog = $console->getHelperSet()->get('dialog'); + $which = $dialog->askAndValidate( + $output, + 'Which invoice? ', + function ($answer) use($files) { + if (! array_key_exists($answer, $files)) { + throw new \RunTimeException( + 'That invoice was not in the list.' + ); + } + + return $answer; + }, + false + ); + $invoice = $files[$which]; + + $output->writeln(''); + } + + $output->writeln("Generating for invoice \"{$invoice}\"..."); + + $invoice_path = $invoice_dir.$invoice.'.md'; + $save_path = $output_dir.$invoice.'.html'; + + if (! file_exists($invoice_path)) { + $output->writeln("Invoice \"{$invoice}\" does not seem to exit under \"{$invoice_src}\"."); + exit; + } + + $markdown_parser = new MarkdownExtraParser; + $html = $markdown_parser->transformMarkdown(file_get_contents($invoice_path)); + + $layout = str_replace('{{ html }}', $html, file_get_contents($layout_path)); + file_put_contents($save_path, $layout); + + $output->writeln(''); + $output->writeln("Invoice generated."); + + if ($output_format == 'pdf') { + $pdf_save_path = substr_replace($save_path, 'pdf', strlen($save_path)-4, 4); + + $process = new Process("wkhtmltopdf {$save_path} {$pdf_save_path}"); + $process->run(); + if (! $process->isSuccessful()) { + throw new \RuntimeException($process->getErrorOutput()); + } + + $output->writeln("PDF saved to: {$pdf_save_path}"); + } else { + $output->writeln("HTML saved to: {$save_path}"); + } + }); +$console->run(); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b1f5198 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "tyler-king/markdown-invoice", + "type": "bin", + "description": "Generates HTML version of my invoices for clients.", + "keywords": ["invoice", "markdown"], + "license": "MIT", + "authors": [ + { + "name": "Tyler King", + "email": "tyler.king@newfie.co" + } + ], + "require": { + "dflydev/markdown": "dev-master", + "symfony/console": "dev-master", + "symfony/process": "dev-master" + }, + "bin": ["bin/markdown-invoice"] +} diff --git a/invoices/layout.html b/invoices/layout.html new file mode 100644 index 0000000..b00cfce --- /dev/null +++ b/invoices/layout.html @@ -0,0 +1,57 @@ + + + + + + + {{ html }} + + \ No newline at end of file