Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created September 26, 2012 09:56
Show Gist options
  • Save coderofsalvation/3787103 to your computer and use it in GitHub Desktop.
Save coderofsalvation/3787103 to your computer and use it in GitHub Desktop.
converts a csv to a nicely designed html file, great for generating reports using cron
#!/bin/bash
# Copyright (c) 2012 Coder of Salvation
#
# 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.
#
# This bashscript-snippet will convert an .csv into a goodlooking html-file.
# I made this because many non-technical users are confused by a .csv file.
#
# What it does:
# it uses asciidoc to setup a basic layout, and uses the csvtable-feature of asciidoc.
#
# Requirements: unix/linux, asciidoc installed
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SELF_ABS=`readlink -f $0`
# # Absolute path this script is in, thus /home/user/bin
SELF_PATH=`dirname $SELF_ABS`
SELF=$( basename $0 )
# main layout of asciidoc article
rowdesc="ID,Columnname1,Columnname2,Columnname3"
asciidoc_src='
Some Report
===========
Author <[email protected]>
last modified '$(date)'
Some Heading
~~~~~~~~~~~~
Lorem ipsum dolor sit amet:
[format="csv",cols="^1,4*2",options="header"]
|===================================================
'$rowdesc'
include::somecsv.csv[]
|===================================================
'
# CSV content (now one is created on the fly, but it could be also a file etc)
csv=$(printf '"foo","bar","foo","bar"\n"foo","bar","foo","bar"\n')
# generate files (csv, asciidoc-txt, html)
echo "$csv" > "$SELF_PATH/report.csv"
echo "$asciidoc_src" > "$SELF_PATH/report.txt"
asciidoc -a data-uri -a icons "$SELF_PATH/report.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment