1# NanoBlogger Article Manager plugin
2
3# How it works:
4# Looks for .txt files in multiple directories.
5# Loads text file:
6# 	1st line becoms article's title (MKPAGE_TITLE).
7# 	everything else becomes content (MKPAGE_CONTENT).
8# Creates the article using the makepage.htm template.
9# Reads alternate title for list from $ARTICLES_TITLE_FILE (1st line).
10# Adds links to NB_ArticleLinks.
11
12# sample code for templates, based off the default stylesheet
13#
14# $NB_Article_Links
15
16# set BASE_URL for links to $ATCLSECTION_DIR
17set_baseurl "./"
18
19# space seperated list of sub-directories inside $BLOG_DIR, where articles are located
20set_articleconf(){
21# e.g. ARTICLES_DIR="articles stories poems long\ name\ with\ spaces"
22: ${ARTICLES_DIR:=articles}
23: ${ARTICLES_SUFFIX:=txt}
24: ${ARTICLES_TEMPLATE:=$NB_TEMPLATE_DIR/$MAKEPAGE_TEMPLATE}
25: ${ARTICLES_FILTERCMD:=sort}
26: ${ARTICLES_FORMAT:=$PAGE_FORMAT}
27: ${ARTICLES_TITLE_FILE=:.articles_title.txt}
28}
29
30# reset basic configs to allow for multiple article configs
31reset_articleconf(){
32ARTICLES_SUFFIX=; ARTICLES_TEMPLATE=; ARTICLES_FORMAT=
33set_articleconf
34}
35
36ARTICLE_PLUGIN_OUTFILE="$BLOG_DIR/$PARTS_DIR/article_links.$NB_FILETYPE"
37
38set_articlelink(){
39articlelink_var="$1"
40article_title=`sed 1q "$BLOG_DIR/$ATCLSECTION_DIR/$articlelink_var"`
41[ -z "$article_title" ] && article_title="$notitle"
42
43# new way
44article_name=`echo "$articlelink_var" |cut -d"." -f 1`
45article_dir=`set_title2link "$article_name"`
46article_file="$article_dir/index.$NB_FILETYPE"
47article_link="$article_dir/$NB_INDEX"
48
49# old way
50#article_file=`chg_suffix "$articlelink_var"`
51#article_link="$article_file"
52}
53
54addalist_name(){
55NB_ArticlesListTitle=
56# Reads alternate title for list from $ARTICLES_TITLE_FILE (1st line).
57[ -f "$BLOG_DIR/$ATCLSECTION_DIR/$ARTICLES_TITLE_FILE" ] &&
58	NB_ArticlesListTitle=`sed 1q $BLOG_DIR/$ATCLSECTION_DIR/$ARTICLES_TITLE_FILE`
59[ -z "$NB_ArticlesListTitle" ] && NB_ArticlesListTitle="$ATCLSECTION_DIR"
60# fallback to our language definition for list's title
61[ ! -z "$templates_articles" ] && [ -z "$NB_ArticlesListTitle" ] &&
62	NB_ArticlesListTitle="$template_articles"
63cat >> "$ARTICLE_PLUGIN_OUTFILE" <<-EOF
64	<div class="sidetitle">
65		$NB_ArticlesListTitle
66	</div>
67EOF
68NB_ArticlesListTitleHTML=$(< "$ARTICLE_PLUGIN_OUTFILE")
69> "$ARTICLE_PLUGIN_OUTFILE"
70}
71
72add_articlelink(){
73	echo '<!--'$MKPAGE_TITLE'--><a href="'${BASE_URL}$ATCLSECTION_DIR/$article_link'">'$MKPAGE_TITLE'</a><br />' >> "$ARTICLE_PLUGIN_OUTFILE"
74	}
75
76create_article(){
77MKPAGE_SRCFILE="$BLOG_DIR/$ATCLSECTION_DIR/$article_srcfile"
78MKPAGE_OUTFILE="$BLOG_DIR/$ATCLSECTION_DIR/$article_file"
79[ "$NB_QUERY" = articles ] || [ "$NB_QUERY" = all ] && rm -f "$MKPAGE_OUTFILE"
80if [ "$MKPAGE_SRCFILE" -nt "$MKPAGE_OUTFILE" ]; then
81	NB_MetaBody=`sed 1d "$BLOG_DIR/$ATCLSECTION_DIR/$article_srcfile"`
82	# modify page content used by make_page function
83	MKPAGE_CONTENT="$NB_MetaBody"
84	# set text formatting for page content
85	MKPAGE_FORMAT="$ARTICLES_FORMAT"
86	make_page "$BLOG_DIR/$ATCLSECTION_DIR/$article_srcfile" "$ARTICLES_TEMPLATE" "$MKPAGE_OUTFILE"
87fi
88}
89
90cycle_articles_for(){
91build_part="$1"
92build_list=`cd "$BLOG_DIR/$ATCLSECTION_DIR"; for articles in *.$ARTICLES_SUFFIX; do echo "$articles"; done`
93[ "$build_list" = "*.$ARTICLES_SUFFIX" ] && build_list=
94article_lines=`echo "$build_list" |grep -n "." |cut -c1-2 |sed -e '/[\:\]/ s///g'`
95for line in ${article_lines[@]}; do
96	article_line=${build_list[$line]}
97	article_srcfile=`echo "$article_line"`
98	if [ -f "$BLOG_DIR/$ATCLSECTION_DIR/$article_srcfile" ]; then
99		set_articlelink "$article_srcfile"
100		MKPAGE_TITLE="$article_title"
101		"$build_part"
102	fi
103done
104}
105
106> "$ARTICLE_PLUGIN_OUTFILE"
107set_articleconf
108for articles_pass in 1 2; do
109	for ATCLSECTION_DIR in ${ARTICLES_DIR[@]}; do
110		if [ -d "$BLOG_DIR/$ATCLSECTION_DIR" ]; then
111			# load articles config file
112			ARTICLE_CONF="$BLOG_DIR/$ATCLSECTION_DIR/article.conf"
113			if [ -f "$ARTICLE_CONF" ]; then
114				reset_articleconf
115				. "$ARTICLE_CONF"
116			fi
117			if [ "$articles_pass" -lt 2 ]; then
118				addalist_name
119				cycle_articles_for add_articlelink
120				NB_ArticleLinksHTML=`$ARTICLES_FILTERCMD "$ARTICLE_PLUGIN_OUTFILE"`
121				cat > "$ARTICLE_PLUGIN_OUTFILE" <<-EOF
122					$NB_ArticlesListTitleHTML
123					<div class="side">
124						$NB_ArticleLinksHTML
125					</div>
126				EOF
127				NB_ArticleLinks=$(< "$ARTICLE_PLUGIN_OUTFILE")
128			else
129				[ -d "$BLOG_DIR/$ATCLSECTION_DIR" ] && nb_msg "$plugins_action $plugins_articles: $BLOG_DIR/$ATCLSECTION_DIR ..."
130				cycle_articles_for create_article
131			fi
132		fi
133	done
134done
135# clear settings for some page plugins, like markdown.sh
136reset_articleconf
137