Skip to content

Commit

Permalink
WIP first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
camelotMachineUser committed Jan 2, 2020
1 parent a0501e9 commit 3d4da8a
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:10

RUN apt-get update && \
apt-get -y install xvfb gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 \
libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 \
libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 \
libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 \
libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget curl && \
rm -rf /var/lib/apt/lists/*

RUN npm install puppeteer markdown-it mustache markdown-it-named-headers cheerio
COPY makepdfs.js /
COPY package.json /
COPY template/ template/
COPY styles/ styles/

RUN fc-cache -fv && \
chmod +x /makepdfs.js && \
mkdir /pdf && \
chmod 777 /pdf && \
ln -s /makepdfs.js /usr/local/bin/makepdfs
CMD [ "makepdfs" ]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Merritt Krakowitzer
Copyright (c) 2020 Merritt Krakowitzer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# A github action for creating PDFs from github markdown

This action creates PDF documents from github markdown

## Inputs

### `markdown_dir`

**Required** Location of markdown files in github repository. Default `doc`.

### `output_dir`

**Required** Location to output PDF files to. Default `tmp`.

## Example usage

```
on: [push]
name: CreatePDFs
jobs:
makepdfs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: mkrakowitzer/actions-makepdfs@master
if: github.ref == 'refs/heads/master'
- uses: actions/upload-artifact@v1
with:
name: platform-architecture-docs
path: tmp
```
19 changes: 19 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# action.yml
name: 'Create PDF'
description: 'Creates PDF files from github markdown'
runs:
using: 'docker'
image: 'Dockerfile'
inputs:
markdown_dir:
description: 'Location of markdown files in github repository'
required: true
default: 'doc'
output_dir:
description: 'Location to output PDF files to'
required: true
default: 'tmp'

branding:
icon: 'activity'
color: 'green'
127 changes: 127 additions & 0 deletions makepdfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env node

const markdown_dir = process.env.INPUT_MARKDOWN_DIR;
const output_dir = process.env.INPUT_OUTPUT_DIR;

'use strict';
var fs = require( 'fs' );
var mddir = '/github/workspace/' + markdown_dir;
var dir = '/github/workspace/' + output_dir + '/';

/*
* Show an error message
*/
function showErrorMessage(msg, error) {
console.log('ERROR: ' + msg);
if (error) {
console.log(error);
}
}

/*
* make html
*/
function makeHtml(data) {
try {
// read files that make up template
var style = fs.readFileSync("/styles/markdown.css", ).toString('utf-8') + fs.readFileSync("/styles/markdown-pdf.css", ).toString('utf-8');
var template = fs.readFileSync("/template/template.html").toString('utf-8');
// compile template
var mustache = require('mustache');

var view = {
style: style,
content: data
};
return mustache.render(template, view);
} catch (error) {
showErrorMessage('makeHtml()', error);
}
}

/*
* make PDF
*/
function makePdf(data,file) {
try {
file = file.replace('.md','');
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch( {
executablePath:'/node_modules/puppeteer/.local-chromium/linux-706915/chrome-linux/chrome',
args: [
'--headless',
'--no-sandbox',
'--disable-setuid-sandbox'
]
} )
const page = await browser.newPage();
await page.goto(data, {waitUntil: 'networkidle2'});
await page.pdf({
path: dir + file + '.pdf',
format: 'A4',
scale: .9,
displayHeaderFooter: true,
margin: {top: 100, bottom:100, right: '50', left: '50'},
footerTemplate: '<div class="pageNumber" style="font-size:8px;width:100%;text-align:center;"></div>',
headerTemplate: '<div class="date" style="font-size:8px;width:100%;text-align:center;"></div>'
});

await browser.close();
})();
} catch (error) {
showErrorMessage('makeHtml()', error);
}
}

function Slug(string) {
try {
var stg = encodeURI(string.trim()
.toLowerCase()
.replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~\`]/g, '')
.replace(/\s+/g, '-')
.replace(/^\-+/, '')
.replace(/\-+$/, ''));
return stg;
} catch (error) {
showErrorMessage('Slug()', error);
}
}

var path = require('path');

if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}

fs.readdir (mddir,function(err, files) {
for (let file of files) {
if (path.extname(file) == '.md') {
var text = fs.readFileSync('doc/' + file).toString('utf-8');
var md = require('markdown-it')({
html: true,
breaks: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
str = hljs.highlight(lang, str, true).value;
} catch (error) {
str = md.utils.escapeHtml(str);

showErrorMessage('markdown-it:highlight', error);
}
} else {
str = md.utils.escapeHtml(str);
}
return '<pre class="hljs"><code><div>' + str + '</div></code></pre>';
}
});
var options = {
slugify: Slug
}
md.use(require('markdown-it-named-headers'), options);
var body = md.render(text);
makePdf('data:text/html;,' + encodeURIComponent(makeHtml(body)),file);
}
}
});
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"puppeteer": "1.15.0",
"commander": "2.20.0",
"markdown-it": "8.4.2"
}
}
55 changes: 55 additions & 0 deletions styles/markdown-pdf.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Markdown PDF CSS
*/

body {
font-family: "Meiryo", "Segoe WPC", "Segoe UI", "SFUIText-Light", "HelveticaNeue-Light", sans-serif, "Droid Sans Fallback";
}

img {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}

pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
border-radius: 3px;
overflow-x: auto;
white-space: pre-wrap;
overflow-wrap: break-word;
}

pre:not(.hljs) {
padding: 23px;
line-height: 19px;
}

blockquote {
background: rgba(127, 127, 127, 0.1);
border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
height: 1.4em;
}

/* for inline code */
:not(pre):not(.hljs) > code {
color: #C9AE75; /* Change the old color so it seems less like an error */
font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
page-break-after: always;
}

@media print {
p {page-break-inside: avoid;}
h1 {page-break-before: always;}
footer {page-break-after: always;}
}
Loading

0 comments on commit 3d4da8a

Please sign in to comment.