Background

Blog is a public text platform, while Diary is a private
shell, I need both. Last post I took down the settings for an
automatically post and publish blog tools, this time I also set up
an automatic diary tools on OS X or Linux. Since my working
platform changed to OS X, I hope it will also help on a Linux
environment.

Features

I define the automatic diary tools as generate and format are
automatically organized by the tools, not the contents. Therefore, it
has the features as follows:

  • create a proper title for the diary file, eg. 2014-03-01.md or 01.03.2014.md or Saturday, 01. March 2014.md or even with time 2014-03-01 11:41:51.md
  • insert markdown to html css color templete reference.
  • joint all the markdown files together to one html file.
  • convert each markdown file to a related html file.
  • convert from html to pdf file, both for one whole pdf and individual pdf file.

Steps

  1. create an empty mardown file and open it with my editor (emacs or vim), the shell script name is new_diary, usage : ./new_diary , then it will generate a file with date or time as its name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
# A shell to create a new diary with some template
datemark=`date +%Y-%m-%d`
filename=${datemark}.md
if [ -e $filename ]
then
echo "$filename existed!"
else
touch $filename
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">" >> $filename
echo "<link href=\"md.css\" rel=\"stylesheet\"></link>" >> $filename
fi

echo -e "*`date +%Y-%m-%d%t%R`*\n\n" >> $filename
emacsclient -t $filename -a ""
  1. joint all the markdown files to one html file, the shell script name is md-joint, usage : ./md-joint then input the file name for the whole one html file
1
2
3
4
5
6
#!/bin/bash
# merge all the *.md files into a single html file

echo -e "merge to file with name:\c"
read filename
cat *.md | markdown > ${filename}.html
  1. convert each .md file to its .html and .pdf file, I created a Makefile to handle this, all I need to do is type make for both html and pdf, or I can choose to generate html by make html or pdf by make pdf.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Makefile
MD = markdown
H2P = ./wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf

SOURCES := $(wildcard *.md)
OBJECTS := $(patsubst %.md, %.html, $(wildcard *.md))
OBJECTS_PDF := $(patsubst %.html, %.pdf, $(wildcard *.html))

all: build

build: html pdf

pdf: $(OBJECTS_PDF)

html: $(OBJECTS)

$(OBJECTS_PDF): %.pdf: %.html
$(H2P) $< $@

$(OBJECTS): %.html: %.md
$(MD) $< > $@

clean:
rm -f $(OBJECTS) $(OBJECTS_PDF)
  1. publish all the pdf files as a complete pdf file with each diary on one page. This script was copied from pdfdir, I did not change any thing of the code. Usage: ./pdf-joint ../DIARY and it will create the DIARY.pdf at the same directory.

So, that’s all, I am happy to share my tools (life style) with all of you, thank you.