Skip to content

Commit

Permalink
Inital
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler King committed Jan 15, 2013
0 parents commit eee56ff
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
composer.lock
vendor/
output/*
invoices/*/*
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
Copyright (C) 2012 Tyler King <tyler.king@newfie.co>

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.
29 changes: 29 additions & 0 deletions 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
116 changes: 116 additions & 0 deletions bin/markdown-invoice
@@ -0,0 +1,116 @@
#!/usr/bin/env php
<?php

if ((! @include __DIR__.'/../../../autoload.php') && (! @include __DIR__.'/../vendor/autoload.php')) {
die('You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use dflydev\markdown\MarkdownExtraParser;

$console = new Application;
$console
->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("<comment>Please choose an invoice in \"{$invoice_src}\"...</comment>");
$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("<comment>{$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("<comment>Generating for invoice \"{$invoice}\"...</comment>");

$invoice_path = $invoice_dir.$invoice.'.md';
$save_path = $output_dir.$invoice.'.html';

if (! file_exists($invoice_path)) {
$output->writeln("<error>Invoice \"{$invoice}\" does not seem to exit under \"{$invoice_src}\".</error>");
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("<info>Invoice generated.</info>");

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("<info>PDF saved to: {$pdf_save_path}</info>");
} else {
$output->writeln("<info>HTML saved to: {$save_path}</info>");
}
});
$console->run();
19 changes: 19 additions & 0 deletions 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"]
}
57 changes: 57 additions & 0 deletions invoices/layout.html
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
/* From https://github.com/simonlc/Markdown-CSS */
html{font-size:100%;overflow-y:scroll;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}
body{color:#444;font-family:Georgia,Palatino,'Palatino Linotype',Times,'Times New Roman',serif;font-size:12px;line-height:1.5em;padding:1em;margin:auto;max-width:42em;background:#fefefe}
a{color:#0645ad;text-decoration:none}
a:visited{color:#0b0080}
a:hover{color:#06e}
a:active{color:#faa700}
a:focus{outline:thin dotted}
a:hover,a:active{outline:0}
::-moz-selection{background:rgba(255,255,0,0.3);color:#000}
::selection{background:rgba(255,255,0,0.3);color:#000}
a::-moz-selection{background:rgba(255,255,0,0.3);color:#0645ad}
a::selection{background:rgba(255,255,0,0.3);color:#0645ad}
p{margin:1em 0}
img{max-width:100%}
h1,h2,h3,h4,h5,h6{font-weight:normal;color:#111;line-height:1em}
h4,h5,h6{font-weight:bold}
h1{font-size:2.5em}
h2{font-size:2em}
h3{font-size:1.5em}
h4{font-size:1.2em}
h5{font-size:1em}
h6{font-size:.9em}
blockquote{color:#666;margin:0;padding-left:3em;border-left:.5em #EEE solid}
hr{display:block;height:2px;border:0;border-top:1px solid #aaa;border-bottom:1px solid #eee;margin:1em 0;padding:0}
pre,code,kbd,samp{color:#000;font-family:monospace,monospace;_font-family:'courier new',monospace;font-size:.98em}
pre{white-space:pre;white-space:pre-wrap;word-wrap:break-word}
b,strong{font-weight:bold}
dfn{font-style:italic}
ins{background:#ff9;color:#000;text-decoration:none}
mark{background:#ff0;color:#000;font-style:italic;font-weight:bold}
sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}
sup{top:-0.5em}
sub{bottom:-0.25em}
ul,ol{margin:1em 0;padding:0 0 0 2em}
li p:last-child{margin:0}
dd{margin:0 0 0 2em}
img{border:0;-ms-interpolation-mode:bicubic;vertical-align:middle}
table{padding:0}
table tr{border-top:1px solid #ccc;background-color:white;margin:0;padding:0}
table tr:nth-child(2n){background-color:#f8f8f8}
table tr th{font-weight:bold;text-align:left;margin:0;padding:6px 13px}
table tr td{border:1px solid #ccc;text-align:left;margin:0;padding:6px 13px}
table tr th :first-child,table tr td :first-child{margin-top:0}
table tr th :last-child,table tr td :last-child{margin-bottom:0}
@media only screen and (min-width:480px){body{font-size:14px}}
@media only screen and (min-width:768px){body{font-size:16px}}
</style>
</head>
<body>
{{ html }}
</body>
</html>

0 comments on commit eee56ff

Please sign in to comment.