nb 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. #! /bin/bash
  2. ############################################################
  3. # NanoBlogger SVN Copyright 2009 n1xt3r (Kevin R. Wood) #
  4. ############################################################
  5. # Last modified: 2011-07-31T16:53:21-04:00
  6. # nanoblogger's version.
  7. VERSION="20.0"
  8. # nanoblogger's copyright
  9. COPYRIGHT="2004-2011/2020-"
  10. # -- hardcoded paths --
  11. # where to expect nanoblogger's base
  12. NB_BASE_DIR=`dirname $0`
  13. # where to expect nanoblogger's documentation
  14. NB_DOC_DIR="$NB_BASE_DIR/docs"
  15. # where to expect nanoblogger's conf file
  16. NB_CFG_DIR="$NB_BASE_DIR"
  17. # nanoblogger's language definitions directory.
  18. NB_LANG_DIR="${NB_BASE_DIR}/lang"
  19. # nanoblogger's module directory.
  20. NB_LIB_DIR="${NB_BASE_DIR}/lib"
  21. # --
  22. # -- modules --
  23. . ${NB_LIB_DIR}/error.sh
  24. . ${NB_LIB_DIR}/tools.sh
  25. . ${NB_LIB_DIR}/config.sh
  26. . ${NB_LIB_DIR}/database.sh
  27. . ${NB_LIB_DIR}/archive.sh
  28. # --
  29. # -- misc --
  30. # setup /dev/null for undesired output
  31. [ -c /dev/null ] && DEV_NULL=/dev/null
  32. : ${DEV_NULL:=$NB_TEMP_DIR/nb_scratch$$-dev_null}
  33. # load specified or default language
  34. load_lang(){
  35. LOADLANG_CODE="$1"
  36. [ -z "$1" ] && LOADLANG_CODE="$NB_LANG"
  37. # always fallback to english
  38. if [ ! -d "${NB_LANG_DIR}/${LOADLANG_CODE}" ]; then
  39. LOADLANG_CODE=en; NB_LANG=en
  40. fi
  41. for lang_file in ${NB_LANG_DIR}/${LOADLANG_CODE}/*.lang; do
  42. . "$lang_file"
  43. done
  44. # configure language for docs
  45. if [ -f "$NB_DOC_DIR/nanoblogger_$LOADLANG_CODE.html" ]; then
  46. NB_DOC_FILE="$NB_DOC_DIR/nanoblogger_$LOADLANG_CODE.html"
  47. elif [ "$LOADLANG_CODE" = en ]; then
  48. # allow for non-existant nanoblogger_en.html
  49. NB_DOC_FILE="$NB_DOC_DIR/nanoblogger.html"
  50. fi
  51. }
  52. # ensure a sane configuration - do or die!
  53. check_config(){
  54. deconfig; load_config
  55. load_lang
  56. [ ! -d "$NB_BASE_DIR" ] &&
  57. die "`basename $0`: '$NB_BASE_DIR' - $checkconf_nobase."
  58. [ -z "$BLOG_DIR" ] && die "$checkconf_noblog"
  59. [ ! -z "$USR_BLOGCONF" ] &&
  60. [ ! -f "$USR_BLOGCONF" ] && die "'$USR_BLOGCONF' - $checkconf_nousrconf"
  61. [ ! -d "$BLOG_DIR" ] && die "'$BLOG_DIR' - $checkconf_noblogdir"
  62. [ ! -d "$NB_DATA_DIR" ] && die "'$NB_DATA_DIR' - $checkconf_nodata"
  63. [ ! -d "$BLOG_DIR/$CACHE_DIR" ] && die "'$CACHE_DIR' - $checkconf_nocache"
  64. [ ! -d "$BLOG_DIR/$PARTS_DIR" ] && die "'$PARTS_DIR' - $checkconf_nocache"
  65. [ ! -d "$NB_TEMPLATE_DIR" ] && die "'$NB_TEMPLATE_DIR' - $checkconf_notemplates"
  66. [ ! -w "$NB_TEMP_DIR" ] &&
  67. die "'$NB_TEMP_DIR' - $nowritedir"
  68. [ ! -w "$BLOG_DIR" ] &&
  69. die "'$BLOG_DIR' - $nowritedir"
  70. # if we're still alive set trap for special temp files
  71. SCRATCH_FILE="$NB_TEMP_DIR/nb_scratch$$"
  72. NB_TEMP_FILES="$NB_TEMP_DIR/nb_entry$$.* $SCRATCH_FILE*"
  73. trap "rm -fr $NB_TEMP_FILES; exit" 0 1 2 3 15
  74. # activate custom file creation mask
  75. [ ! -z "$NB_UMASK" ] && umask $NB_UMASK
  76. }
  77. # edit $BLOG_CONF
  78. config_weblog(){
  79. PREV_CHRONORDER="$CHRON_ORDER"
  80. nb_edit "$BLOG_CONF"
  81. # check if file's been modified since opened
  82. [ ! -N "$BLOG_CONF" ] && die "$configweblog_nomod"
  83. deconfig; load_config
  84. # set flag to resort databases if chronological order has changed
  85. # NOTE: rendered ineffective outside configure action
  86. [ "$PREV_CHRONORDER" != "$CHRON_ORDER" ] &&
  87. RESORT_DATABASE=1
  88. }
  89. # load specified plugins (defaults to all) from $PLUGINS_DIR
  90. load_plugins(){
  91. PLUGIN_DIR="$1"
  92. PLUGINS_LIST=( ${@:1} )
  93. add_plugin_script(){
  94. plugin_file="$1"
  95. if [ -f "$plugin_file" ]; then
  96. plugin_basefile="${PLUGIN_DIR}/${plugin_file##*\/}"
  97. plugin_basefile="${plugin_basefile##\/}"
  98. plugin_rmdir="${PLUGIN_DIR}/"
  99. plugin_rmfile="${plugin_rmdir##\/}${plugin_basefile##*\/}"
  100. # remove plugins while we add them to eliminate duplications
  101. plugin_scripts=(${plugin_scripts[@]//$plugin_rmfile/} "$plugin_basefile" )
  102. fi
  103. }
  104. # determine the plugin direcory
  105. [ ! -z "$PLUGIN_DIR" ] && PLUGIN_DIR="/$1"
  106. # cleanup any trailing slashes in $USR_PLUGINSDIR
  107. USR_PLUGINSDIR=${USR_PLUGINSDIR%%\/}
  108. # allow loading plugins individually
  109. if [ -f "${USR_PLUGINSDIR}${PLUGIN_DIR}" ] || [ -f "${PLUGINS_DIR}${PLUGIN_DIR}" ]; then
  110. PLUGIN_BASEFILE="${PLUGIN_DIR##*\/}"
  111. PLUGIN_BASEDIR="${PLUGIN_DIR//\/${PLUGIN_BASEFILE}/}"
  112. PLUGIN_DIR="$PLUGIN_BASEDIR"
  113. fi
  114. # determine the plugins directory list
  115. if [ -d "${USR_PLUGINSDIR}${PLUGIN_DIR}" ] && [ -d "${PLUGINS_DIR}${PLUGIN_DIR}" ]; then
  116. if [ "${PLUGINS_DIR}" != "${USR_PLUGINSDIR}" ]; then
  117. PLUGINSDIR_LIST="${USR_PLUGINSDIR}${PLUGIN_DIR} ${PLUGINS_DIR}${PLUGIN_DIR}"
  118. else
  119. PLUGINSDIR_LIST="${PLUGINS_DIR}${PLUGIN_DIR}"
  120. fi
  121. elif [ -d "${USR_PLUGINSDIR}${PLUGIN_DIR}" ]; then
  122. PLUGINSDIR_LIST="${USR_PLUGINSDIR}${PLUGIN_DIR}"
  123. elif [ -d "${PLUGINS_DIR}${PLUGIN_DIR}" ]; then
  124. PLUGINSDIR_LIST="${PLUGINS_DIR}${PLUGIN_DIR}"
  125. fi
  126. # initialise the array of plugins
  127. plugin_scripts=()
  128. for plugin_path in $PLUGINSDIR_LIST; do
  129. # break out of loop on bogus plugin path with last error value
  130. [ ! -d "$plugin_path" ] && return
  131. # add single plugin specified to load then drop out of loop
  132. if [ ! -z "$PLUGIN_BASEFILE" ]; then
  133. add_plugin_script "$plugin_path/$PLUGIN_BASEFILE"
  134. break
  135. fi
  136. # was more than one plugin name defined?
  137. PLUGINLIST_DEFINED=`echo "${PLUGINS_LIST[*]}" |grep -c '.[ ].'`
  138. if [ "$PLUGINLIST_DEFINED" != 0 ] ; then
  139. # yes, so we add them in order specified (removing any commas)
  140. for plugin in ${PLUGINS_LIST[@]//\,/}; do
  141. nb_plugin="$plugin_path/${plugin//[ ][ ]/}.sh"
  142. add_plugin_script "$nb_plugin"
  143. done
  144. else
  145. # no plugins specified, so get them all
  146. for nb_plugin in "$plugin_path"/*.sh ; do
  147. add_plugin_script "$nb_plugin"
  148. done
  149. fi
  150. done
  151. PLUGIN_BASEFILE=
  152. # load user and main plugins in alpha-numeric order (0-9, A-z)
  153. for nb_plugin in ${plugin_scripts[@]}; do
  154. #nb_msg "loading ../$nb_plugin ..."
  155. # allow user plugins to override and disable main plugins
  156. nbpl_prefix=${nb_plugin%%.sh}
  157. nbpl_glob=`echo "${PLUGINS_DIR}/${nbpl_prefix}".*`
  158. nbupl_glob=`echo "${USR_PLUGINSDIR}/${nbpl_prefix}".*`
  159. if [ -f "${PLUGINS_DIR}/$nb_plugin" ] && [ -f "${USR_PLUGINSDIR}/$nb_plugin" ]; then
  160. # plugin.sh exists in both places, user plugin.sh takes precedence
  161. #nb_msg "loading ... ${USR_PLUGINSDIR}/$nb_plugin"
  162. . "${USR_PLUGINSDIR}/$nb_plugin"
  163. elif [ -f "$nbupl_glob" ] && [ ! -f "${USR_PLUGINSDIR}/${nb_plugin}" ]; then # something other than plugin.sh exists
  164. continue # stay in loop, but skip to next in list
  165. elif [ -f "${USR_PLUGINSDIR}/$nb_plugin" ]; then # plugin.sh only exists in one place
  166. #nb_msg "loading ... ${USR_PLUGINSDIR}/$nb_plugin"
  167. . "${USR_PLUGINSDIR}/$nb_plugin"
  168. elif [ -f "$nbpl_glob" ] && [ ! -f "${PLUGINS_DIR}/${nb_plugin}" ]; then # something other than plugin.sh exists
  169. continue # stay in loop, but skip to next in list
  170. elif [ -f "${PLUGINS_DIR}/$nb_plugin" ]; then # plugin.sh only exists in on place
  171. #nb_msg "loading ... ${PLUGINS_DIR}/$nb_plugin"
  172. . "${PLUGINS_DIR}/$nb_plugin"
  173. fi
  174. done
  175. }
  176. # filter content through a template
  177. load_template(){
  178. TEMPLATE_FILE="$1"
  179. if [ -f "$TEMPLATE_FILE" ]; then
  180. # prefix lines with an X
  181. TEMPLATE=`sed -e '/^/ s//X: /' < "$TEMPLATE_FILE"`
  182. # remove X's and source variables into a temp file
  183. cat > "$SCRATCH_FILE".template <<-EOF
  184. sed -e '/^X:[ ]/ s///' <<TMPL
  185. $TEMPLATE
  186. TMPL
  187. EOF
  188. TEMPLATE_DATA=`. "$SCRATCH_FILE".template`
  189. else
  190. die "'$TEMPLATE_FILE' - $loadtemplate_nofile"
  191. fi
  192. }
  193. # write template data to stdout
  194. write_template(){
  195. cat <<-EOF
  196. $TEMPLATE_DATA
  197. EOF
  198. }
  199. # build the weblog
  200. build_weblog(){
  201. # pre-processing for extensive update options
  202. if [ "$QUERY_WEBLOG" != 1 ]; then
  203. NB_QUERY="$updweblog_type"; db_query="$NB_QUERY"
  204. fi
  205. case "$updweblog_type" in
  206. article|article[a-z]) NB_QUERY=article; db_query=main;;
  207. feed|feed[a-z]) NB_QUERY=feed; db_query=main;;
  208. tag|tag[a-z]) if [ ! -z "$update_idsel" ]; then
  209. db_query=; cat_num="$update_idsel"; fi;;
  210. esac
  211. init_update(){
  212. # add welcome message with category id 1 (nanoblogger-help) to new weblogs
  213. if [ -f "$BLOG_DIR/.nb_newblogdir" ]; then
  214. # force full update
  215. UPDATE_WEBLOG=1; NB_QUERY=all
  216. import_file "$NB_BASE_DIR/welcome-to-nb.txt"
  217. old_catnum="$cat_num"; cat_num=1
  218. add_entry; cat_num="$old_catnum"
  219. rm -f "$BLOG_DIR/.nb_newblogdir"
  220. fi
  221. # for clean build remove all generated files
  222. if [ "$UPDATE_WEBLOG" = 1 ] && [ "$NB_QUERY" = all ] && [ -z "$cat_num" ] && \
  223. ! [[ $NB_UPDATE == *arch ]]; then
  224. nb_msg "$buildweblog_all"
  225. # confirm removal of any previously generated files
  226. [ "$BLOG_INTERACTIVE" = 1 ] && confirm_action
  227. # use "--force" to rebuild databases
  228. [ "$FORCE_UPDATE" = 1 ] && query_db rebuild
  229. # use "--force" to clear cached data
  230. for update_dir in "$ARCHIVES_DIR" "$PARTS_DIR"; do
  231. [ ! -z "$update_dir" ] && [ -d "$BLOG_DIR/$update_dir" ] &&
  232. rm -fr "$BLOG_DIR"/"$update_dir"/*
  233. done
  234. fi
  235. # update master.db
  236. if [ "$NB_QUERY" != main ]; then
  237. nb_msg "$querydb_init"
  238. query_db master
  239. fi
  240. # update special query results for months and years
  241. query_db years
  242. query_db months
  243. query_db days
  244. # generate our update list from the query
  245. if [ -z "${UPDATE_LIST[*]}" ]; then
  246. # reassert any previously specified categories
  247. [ ! -z "$cat_num" ] && db_catquery=`cat_id "$cat_num"`
  248. # query falls back to default query mode
  249. [ -z "$NB_QUERY" ] && NB_QUERY="$QUERY_MODE"
  250. db_query="$NB_QUERY"
  251. query_db "$db_query" "$db_catquery"
  252. UPDATE_LIST=(${DB_RESULTS[*]})
  253. fi
  254. # only update specified categories upon forced updates
  255. if [ -z $cat_num ] && ! [[ $NB_UPDATE == *arch ]]; then
  256. find_categories "${UPDATE_LIST[*]}"
  257. else
  258. find_categories
  259. fi
  260. # update previous entry's nav. links for new entries
  261. if [ ! -z "$New_EntryFile" ]; then
  262. findba_entries "$New_EntryFile" "${MASTER_DB_RESULTS[*]}"
  263. set_entrynavlinks prev "$before_entry"
  264. set_entrynavlinks next "$after_entry"
  265. [ ! -z "$prev_entry" ] &&
  266. echo "$prev_entry" >> "$SCRATCH_FILE".newentry_list
  267. [ ! -z "$next_entry" ] &&
  268. echo "$next_entry" >> "$SCRATCH_FILE".newentry_list
  269. UPDATE_LIST=(`sort -u "$SCRATCH_FILE".newentry_list`)
  270. fi
  271. }
  272. init_plugins(){
  273. # setup cache for plugins
  274. export CACHE_TYPE=plugin
  275. # always load main plugins
  276. load_plugins
  277. }
  278. init_main(){
  279. # build main weblog index
  280. nb_msg "$buildweblog_main"
  281. export CACHE_TYPE=main
  282. # reset UPDATE_LIST so main index gets updated
  283. query_db "$MAINPAGE_QUERY" nocat
  284. UPDATE_LIST=(${DB_RESULTS[*]})
  285. paginate "$MAINPAGE_QUERY" nocat "$MAX_MAINPAGE_ENTRIES" "$MAIN_TEMPLATE" \
  286. "$ENTRY_TEMPLATE" "$BLOG_DIR/" "$NB_INDEXFILE"
  287. if [ "$NB_QUERY" != main ]; then
  288. # remove any expired cache data to save disk space
  289. if [ "$BLOG_CACHEMNG" != 0 ]; then
  290. nb_msg "$buildweblog_cache"
  291. update_cache expired
  292. fi
  293. fi
  294. # load post plugins
  295. load_plugins post
  296. if [ "$SHOW_TIMES" = 1 ] && [ "$VERBOSE" != 0 ]; then
  297. times
  298. fi
  299. }
  300. nb_msg "$buildweblog_files"
  301. case "$NB_QUERY" in
  302. tag|tag[a-z]|cat)
  303. [ -z "$cat_num" ] && NB_UPDATE=catarch; NB_QUERY=all;
  304. init_update; init_plugins; build_catarchives; init_main
  305. ;;
  306. main) db_query=main; init_update; init_plugins; init_main
  307. ;;
  308. *) # build/update archives based on the query results
  309. if [ "$NB_QUERY" != main ]; then
  310. init_update; init_plugins;
  311. build_archives; init_main;
  312. fi
  313. ;;
  314. esac
  315. }
  316. # add new entry
  317. add_entry(){
  318. # acquire and validate specified categories
  319. if [ ! -z "$cat_num" ]; then
  320. db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  321. nb_msg "$addentry_catinfo $cat_num ..."
  322. fi
  323. nb_msg "$addentry_action"
  324. # generate formatted date string for entry's metadata
  325. meta_timestamp; NB_EntryDate="$NB_MetaDate"
  326. # load user specified timestamp
  327. if [ -f "$NB_EditFile" ]; then
  328. read_metadata TIMESTAMP "$NB_EditFile"
  329. New_TimeStamp="$METADATA"
  330. fi
  331. New_EntryFile="$NB_MetaTimeStamp.$NB_DATATYPE"
  332. # dump all custom modications from edit file to new entry's data file
  333. if [ -f "$NB_EditFile" ]; then
  334. cat "$NB_EditFile" > "$NB_DATA_DIR/$New_EntryFile"
  335. elif [ -f "$IMPORT_FILE" ]; then
  336. # dump all custom modifications from import file to new entry's data file
  337. cat "$IMPORT_FILE" > "$NB_DATA_DIR/$New_EntryFile"
  338. fi
  339. # save date metadata to entry's data file
  340. [ -f "$NB_DATA_DIR/$New_EntryFile" ] &&
  341. write_var DATE "$NB_EntryDate" "$NB_DATA_DIR/$New_EntryFile"
  342. # use specified timestamp as entry's new date
  343. chg_entrydate "$New_EntryFile" "$New_TimeStamp"
  344. [ ! -z "$New_EntryDateFile" ] &&
  345. New_EntryFile="$New_EntryDateFile"
  346. # add to specified categories
  347. if [ ! -z "$cat_num" ]; then
  348. for cat_db in $db_catquery; do
  349. update_catdb "$New_EntryFile" "$NB_DATA_DIR/$cat_db"
  350. done
  351. fi
  352. echo "$New_EntryFile" >> "$SCRATCH_FILE".newentry_list
  353. UPDATE_LIST=(`sort -u "$SCRATCH_FILE".newentry_list`)
  354. find_categories "${UPDATE_LIST[*]}"
  355. nb_msg "$querydb_update"
  356. update_maindb "$New_EntryFile" "$NB_DATA_DIR/master.db"
  357. update_categories
  358. resort_categories
  359. resort_db "$NB_DATA_DIR/master.db"
  360. }
  361. # edit entry or tag by id number
  362. edit_weblog(){
  363. case "$editweblog_type" in
  364. entry) # edit entries
  365. NUMVAR=`echo "$edit_idsel" |grep '[0-9]' |sed -e '/[\,]/ s// /g; /[A-Z,a-z\)\.\-]/d'`
  366. [ -z "$NUMVAR" ] && die "$novalid_entryid"
  367. db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  368. # find entry id(s) from specified query
  369. [ ! -z "$NB_QUERY" ] && db_query="$NB_QUERY"
  370. query_db "$db_query" "$db_catquery"; ENTRY_LIST=(${DB_RESULTS[*]})
  371. for entry_id in $NUMVAR; do
  372. # adjust offset by 1 for arrays (1 = 0)
  373. ((entry_id--))
  374. Edit_EntryFile=${ENTRY_LIST[$entry_id]}
  375. [ ! -f "$NB_DATA_DIR/$Edit_EntryFile" ] && die "$invalid_entryid $edit_idsel"
  376. done
  377. > "$SCRATCH_FILE".edit_weblog
  378. # for chg_entrydate
  379. [ ! -z "$cat_num" ] && nb_msg "$editweblog_catinfo $cat_num"
  380. for entry_id in $NUMVAR; do
  381. ((entry_id--)) # adjust offset by 1 for arrays (1 = 0)
  382. Edit_EntryFile=${ENTRY_LIST[$entry_id]}
  383. read_metadata TITLE "$NB_DATA_DIR/$Edit_EntryFile"
  384. OldEdit_EntryTitle="$METADATA"
  385. # write any user metadata
  386. write_var "$USR_METAVAR" "$USR_SETVAR" "$NB_DATA_DIR/$Edit_EntryFile"
  387. nb_edit -p "$NB_DATA_DIR/$Edit_EntryFile"
  388. # validate metafile
  389. check_metavars "TITLE: AUTHOR: DATE: BODY: $METADATA_CLOSEVAR" \
  390. "$NB_DATA_DIR/$Edit_EntryFile"
  391. # load user specified timestamp
  392. read_metadata TIMESTAMP "$NB_DATA_DIR/$Edit_EntryFile"
  393. New_TimeStamp="$METADATA"
  394. # use specified timestamp as entry's new date
  395. chg_entrydate "$Edit_EntryFile" "$New_TimeStamp"
  396. if [ -f "$NB_DATA_DIR/$New_EntryDateFile" ]; then
  397. Old_EntryFile="$Edit_EntryFile"
  398. Edit_EntryFile="$New_EntryDateFile"
  399. fi
  400. # add timestamp modified entries to update list
  401. if [ ! -z "$Old_EntryFile" ]; then
  402. echo "$Old_EntryFile" >> "$SCRATCH_FILE".edit_weblog
  403. nb_msg "$querydb_update"
  404. find_categories "$Old_EntryFile"
  405. for olde_catdb in ${CAT_LIST[*]}; do
  406. delete_db "$Old_EntryFile" "$NB_DATA_DIR/$olde_catdb"
  407. if [ ! -z "$New_TimeStamp" ]; then # update with new timestamp
  408. update_catdb "$Edit_EntryFile" "$NB_DATA_DIR/$olde_catdb"
  409. fi
  410. done
  411. delete_db "$Old_EntryFile" "$NB_DATA_DIR/master.db"
  412. update_maindb "$Edit_EntryFile" "$NB_DATA_DIR/master.db"
  413. update_categories "$Edit_EntryFile"
  414. resort_categories
  415. resort_db "$NB_DATA_DIR/master.db"
  416. fi
  417. # use cached version for 'newer than' comparison
  418. nt_cachefile=`echo "$BLOG_DIR/$CACHE_DIR/$Edit_EntryFile".*`
  419. nt_cachefile=${nt_cachefile//[ ]*/}
  420. if [ "$NB_DATA_DIR/$Edit_EntryFile" -nt "$nt_cachefile" ]; then
  421. read_metadata TITLE "$NB_DATA_DIR/$Edit_EntryFile"
  422. NewEdit_EntryTitle="$METADATA"
  423. # update previous and next entries of modified entry
  424. query_db master
  425. findba_entries "$Edit_EntryFile" "${MASTER_DB_RESULTS[*]}"
  426. set_entrynavlinks prev "$before_entry"
  427. set_entrynavlinks next "$after_entry"
  428. [ ! -z "$prev_entry" ] &&
  429. echo "$prev_entry" >> "$SCRATCH_FILE".edit_weblog
  430. [ ! -z "$next_entry" ] &&
  431. echo "$next_entry" >> "$SCRATCH_FILE".edit_weblog
  432. echo "$Edit_EntryFile" >> "$SCRATCH_FILE".edit_weblog
  433. # remove old cache data for clean build
  434. rm -f "$BLOG_DIR/$CACHE_DIR/$Edit_EntryFile".*
  435. # remove old entry archive directory
  436. set_entrylink "$Edit_EntryFile"
  437. Delete_EntryArchiveDir="$BLOG_DIR/$ARCHIVES_DIR/$entry_dir/$entry_linkname"
  438. [ ! -z "$entry_linkname" ] && [ -d "$Delete_EntryArchiveDir" ] &&
  439. rm -fr "$Delete_EntryArchiveDir"
  440. fi
  441. done
  442. NB_QUERY=; UPDATE_LIST=(`sort -u "$SCRATCH_FILE".edit_weblog`)
  443. if [ ! -z "${UPDATE_LIST[*]}" ]; then
  444. nb_msg "$editweblog_action"
  445. find_categories "${UPDATE_LIST[*]}"
  446. resort_categories
  447. build_weblog
  448. else
  449. die "$editweblog_noaction"
  450. fi
  451. ;;
  452. meta-file|file) # edit files
  453. nb_draft "$edit_idsel"
  454. ;;
  455. tag|cat) # edit categories
  456. cat_num="$edit_idsel"; cat_var=`echo "$cat_num" |sed -e '/,/d'`
  457. [ -z "$cat_var" ] && die "$editweblog_onecat"
  458. db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  459. nb_msg "$editweblog_catinfo $cat_num"
  460. if [ ! -z "$USR_TITLE" ]; then
  461. set_catlink "$db_catquery"
  462. [ ! -z "$category_dir" ] && [ -d "$BLOG_DIR/$ARCHIVES_DIR/$category_dir" ] &&
  463. rm -fr "$BLOG_DIR/$ARCHIVES_DIR/$category_dir"
  464. new_usrtitle=`translit_text "$USR_TITLE"`
  465. nb_msg "$editweblog_title '$new_usrtitle' ..."
  466. echo "$new_usrtitle" > "$NB_DATA_DIR/$db_catquery".tmp && \
  467. sed 1d "$NB_DATA_DIR/$db_catquery" >> "$NB_DATA_DIR/$db_catquery".tmp && \
  468. mv "$NB_DATA_DIR/$db_catquery".tmp "$NB_DATA_DIR/$db_catquery"
  469. NB_QUERY=; UPDATE_LIST=(`sed 1d "$NB_DATA_DIR/$db_catquery" |sort -u`)
  470. find_categories "${UPDATE_LIST[*]}"
  471. resort_categories
  472. build_weblog; exit
  473. else
  474. die "$editweblog_nomod"
  475. fi
  476. ;;
  477. *) die "$main_badopts $editweblog_type";;
  478. esac
  479. }
  480. # delete entry or category by id number
  481. delete_weblog(){
  482. # find entry id(s) from specified query
  483. [ ! -z "$NB_QUERY" ] && db_query="$NB_QUERY"
  484. db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  485. cat_list="$db_catquery"
  486. case "$delweblog_type" in
  487. entry) # delete entries
  488. NUMVAR=`echo "$delete_idsel" |grep '[0-9]' |sed -e '/[\,]/ s// /g; /[A-Z,a-z\)\.\-]/d'`
  489. [ -z "$NUMVAR" ] && die "$novalid_entryid"
  490. if [ ! -z "$cat_list" ]; then
  491. CATNUMVAR=`echo "$cat_num" |grep '[0-9]' |sed -e '/[\,]/ s// /g; /[A-Z,a-z\)\.\-]/d'`
  492. [ -z "$CATNUMVAR" ] &&
  493. die "$deleteweblog_onecat"
  494. fi
  495. [ ! -z "$NB_QUERY" ] && db_query="$NB_QUERY"
  496. query_db "$db_query" "$cat_list"; ENTRY_LIST=(${DB_RESULTS[*]})
  497. for entry_id in $NUMVAR; do
  498. # adjust offset by 1 for arrays (1 = 0)
  499. old_entryid="$entry_id"
  500. ((entry_id--))
  501. Delete_EntryFile=${ENTRY_LIST[$entry_id]}
  502. [ ! -f "$NB_DATA_DIR/$Delete_EntryFile" ] && die "$invalid_entryid $delete_idsel"
  503. read_metadata TITLE "$NB_DATA_DIR/$Delete_EntryFile"; Delete_EntryTitle=`echo "${METADATA:0:25}"`..
  504. delete_titles="$delete_titles $old_entryid=$Delete_EntryTitle,"
  505. done
  506. delete_titles=`echo "$delete_titles" |sed -e '/^[ ]/ s///; /[\,]$/ s///'`
  507. [ ! -z "$cat_num" ] && nb_msg "$deleteweblog_catinfo $cat_num"
  508. nb_msg "$deleteweblog_delentry $delete_idsel ($delete_titles) ..."
  509. # confirm delete in interactive mode
  510. [ "$BLOG_INTERACTIVE" = 1 ] && confirm_action
  511. # initialize master.db
  512. if [ ! -z "$NB_QUERY" ]; then
  513. db_query="$NB_QUERY"
  514. else
  515. db_query=master
  516. fi
  517. query_db "$db_query"
  518. > "$SCRATCH_FILE"; > "$SCRATCH_FILE.x-update"
  519. nb_msg "$querydb_update"
  520. for entry_id in $NUMVAR; do
  521. # adjust offset by 1 for arrays (1 = 0)
  522. ((entry_id--))
  523. Delete_EntryFile=${ENTRY_LIST[$entry_id]}
  524. if [ -f "$NB_DATA_DIR/$Delete_EntryFile" ]; then
  525. echo "$Delete_EntryFile" >> "$SCRATCH_FILE"
  526. if [ ! -z "${cat_list[*]}" ]; then
  527. # deletes entry from categories
  528. for cat_db in ${cat_list[@]}; do
  529. cat_match=`grep -c "$Delete_EntryFile" "$NB_DATA_DIR/$cat_db"`
  530. [ "$cat_match" -ge 1 ] &&
  531. echo "$cat_db" >> "$SCRATCH_FILE.x-catdbs"
  532. delete_db "$Delete_EntryFile" "$NB_DATA_DIR/$cat_db"
  533. done
  534. update_maindb "$Delete_EntryFile" "$NB_DATA_DIR/master.db"
  535. else
  536. # permanently deletes entry
  537. for cat_db in ${db_categories[@]}; do
  538. cat_match=`grep -c "$Delete_EntryFile" "$NB_DATA_DIR/$cat_db"`
  539. [ "$cat_match" -ge 1 ] &&
  540. echo "$cat_db" >> "$SCRATCH_FILE.x-catdbs"
  541. delete_db "$Delete_EntryFile" "$NB_DATA_DIR/$cat_db"
  542. done
  543. set_entrylink "$Delete_EntryFile"
  544. Delete_PermalinkFile="$BLOG_DIR/$ARCHIVES_DIR/$permalink_file"
  545. Delete_EntryArchiveDir="$BLOG_DIR/$ARCHIVES_DIR/$entry_dir/$entry_linkname"
  546. # delete permalink file
  547. [ -f "$Delete_PermalinkFile" ] && rm -fr "$Delete_PermalinkFile"
  548. # delete permalink directory
  549. [ ! -z "$entry_linkname" ] && [ -d "$Delete_EntryArchiveDir" ] &&
  550. rm -fr "$Delete_EntryArchiveDir"
  551. # delete old cache data
  552. rm -f "$BLOG_DIR/$CACHE_DIR/$Delete_EntryFile".*
  553. # update previous and next entries of deleted entry
  554. query_db master
  555. findba_entries "$Delete_EntryFile" "${MASTER_DB_RESULTS[*]}"
  556. set_entrynavlinks prev "$before_entry"
  557. set_entrynavlinks next "$after_entry"
  558. delete_db "$Delete_EntryFile" "$NB_DATA_DIR/master.db"
  559. rm -f "$NB_DATA_DIR/$Delete_EntryFile"
  560. echo "$Delete_EntryFile" >> "$SCRATCH_FILE.x-entries"
  561. # hide prev and next entries from category updates
  562. [ ! -z "$prev_entry" ] &&
  563. echo "$prev_entry" >> "$SCRATCH_FILE.x-update"
  564. [ ! -z "$next_entry" ] &&
  565. echo "$next_entry" >> "$SCRATCH_FILE.x-update"
  566. fi
  567. fi
  568. done
  569. NB_QUERY=; UPDATE_LIST=(`sort -u "$SCRATCH_FILE"`)
  570. # update all categories with removed entries
  571. [ ! -z "${UPDATE_LIST[*]}" ] &&
  572. find_categories "${UPDATE_LIST[*]}"
  573. [ -f "$SCRATCH_FILE.x-catdbs" ] && CAT_XLIST=(`sort -u "$SCRATCH_FILE.x-catdbs"`)
  574. [ ! -z "${CAT_XLIST[*]}" ] &&
  575. CAT_LIST=(`for xcatdb in ${CAT_LIST[@]} ${CAT_XLIST[@]}; do echo $xcatdb; done |sort -u`)
  576. # prune all removed entries from update list
  577. if [ -f "$SCRATCH_FILE.x-entries" ]; then
  578. for xentry in $(< "$SCRATCH_FILE.x-entries"); do
  579. sed -e '/'$xentry'/d' "$SCRATCH_FILE".x-update > "$SCRATCH_FILE".x-update.new && \
  580. mv "$SCRATCH_FILE".x-update.new "$SCRATCH_FILE".x-update
  581. done
  582. UPDATE_LIST=(`sort -u "$SCRATCH_FILE.x-update"`)
  583. fi
  584. update_categories
  585. resort_categories
  586. build_weblog
  587. ;;
  588. tag|cat) # delete categories
  589. cat_num="$delete_idsel"
  590. [ -z "$cat_num" ] && die "$checkcatid_novalid"
  591. db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  592. cat_list="$db_catquery"
  593. nb_msg "$deleteweblog_delcat $cat_num ..."
  594. # confirm delete in interactive mode
  595. [ "$BLOG_INTERACTIVE" = 1 ] && confirm_action
  596. query_db "$db_query" "${cat_list[*]}"
  597. echo "${DB_RESULTS[*]}" > $SCRATCH_FILE
  598. for cat_db in ${cat_list[@]}; do
  599. Delete_CatDBFile="$cat_db"
  600. [ -f "$NB_DATA_DIR/$cat_db" ] && set_catlink "$cat_db"
  601. [ ! -z "$category_dir" ] && [ -d "$BLOG_DIR/$ARCHIVES_DIR/$category_dir" ] &&
  602. rm -fr "$BLOG_DIR/$ARCHIVES_DIR/$category_dir"
  603. rm -f "$NB_DATA_DIR/$cat_db" "$BLOG_DIR/$ARCHIVES_DIR"/`chg_suffix "$cat_db"`
  604. done; cat_num=
  605. NB_QUERY=; UPDATE_LIST=(`sort -u "$SCRATCH_FILE"`)
  606. [ ! -z "${UPDATE_LIST[*]}" ] &&
  607. find_categories "${UPDATE_LIST[*]}"
  608. nb_msg "$querydb_update"
  609. for oldcat_dbitem in ${UPDATE_LIST[*]}; do
  610. update_maindb "$oldcat_dbitem" "$NB_DATA_DIR/master.db"
  611. done
  612. resort_db "$NB_DATA_DIR/master.db"
  613. build_weblog; exit
  614. ;;
  615. *) die "$main_badopts $delweblog_type";;
  616. esac
  617. }
  618. # assign categories to entries
  619. cat_entry(){
  620. NUMVAR=`echo "$catentry_num" |grep '[0-9]' |sed -e '/[\,]/ s// /g; /[A-Z,a-z\)\.\-]/d'`
  621. [ -z "$NUMVAR" ] && die "$novalid_entryid"
  622. [ ! -z "$NB_QUERY" ] && db_query="$NB_QUERY"
  623. query_db "$db_query"
  624. for entry_id in $NUMVAR; do
  625. # adjust offset by 1 for arrays (1 = 0)
  626. old_entryid="$entry_id"
  627. ((entry_id--))
  628. Cat_EntryFile=${DB_RESULTS[$entry_id]}
  629. [ ! -f "$NB_DATA_DIR/$Cat_EntryFile" ] && die "$invalid_entryid $catentry_num"
  630. read_metadata TITLE "$NB_DATA_DIR/$Cat_EntryFile"; Cat_EntryTitle=`echo "${METADATA:0:25}"`..
  631. catentry_titles="$catentry_titles $old_entryid=$Cat_EntryTitle,"
  632. done
  633. catentry_titles=`echo "$catentry_titles" |sed -e '/^[ ]/ s///; /[\,]$/ s///'`
  634. nb_msg "$catentry_catinfo $cat_num ..."
  635. nb_msg "$catentry_catging $catentry_num ($catentry_titles) ..."
  636. db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"; [ -z "$cat_num" ] && die "$catentry_catfirst"
  637. # confirm category in interactive mode
  638. [ "$BLOG_INTERACTIVE" = 1 ] && confirm_action
  639. > "$SCRATCH_FILE"
  640. nb_msg "$querydb_update"
  641. query_db "$db_query"
  642. for entry_id in $NUMVAR; do
  643. # adjust offset by 1 for arrays (1 = 0)
  644. ((entry_id--))
  645. Cat_EntryFile=${DB_RESULTS[$entry_id]}
  646. if [ -f "$NB_DATA_DIR/$Cat_EntryFile" ]; then
  647. echo "$Cat_EntryFile" >> "$SCRATCH_FILE"
  648. if [ ! -z "${cat_list[*]}" ]; then
  649. for cat_db in ${cat_list[@]}; do
  650. update_catdb "$Cat_EntryFile" "$NB_DATA_DIR/$cat_db"
  651. done
  652. fi
  653. update_maindb "$Cat_EntryFile" "$NB_DATA_DIR/master.db"
  654. fi
  655. done
  656. NB_QUERY=; UPDATE_LIST=(`sort -u "$SCRATCH_FILE"`)
  657. [ ! -z "${UPDATE_LIST[*]}" ] &&
  658. find_categories "${UPDATE_LIST[*]}"
  659. update_categories
  660. resort_categories
  661. resort_db "$NB_DATA_DIR/master.db"
  662. build_weblog
  663. }
  664. # list entries and categories
  665. list_weblog(){
  666. query_db; db_query="$NB_QUERY"
  667. # pre-processing for extensive list options
  668. if [ "$QUERY_WEBLOG" != 1 ]; then
  669. NB_QUERY="$listweblog_type"; db_query="$NB_QUERY"
  670. fi
  671. case "$listweblog_type" in
  672. tag|tag[a-z]|cat) if [ ! -z "$list_idsel" ]; then
  673. db_query=; cat_num="$list_idsel"; fi;;
  674. esac
  675. db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  676. explode_list(){
  677. EXPLODE_RESULTS=($1)
  678. list_entryid=0
  679. entry_items=${#EXPLODE_RESULTS[*]}
  680. id=0
  681. while [ "$id" -lt "$entry_items" ]; do
  682. let list_entryid=${list_entryid}+1
  683. entry=${EXPLODE_RESULTS[$id]%%>[0-9]*}
  684. entry_cat=${EXPLODE_RESULTS[$id]##*\>}
  685. read_metadata TITLE "$NB_DATA_DIR/$entry"; NB_EntryTitle="$METADATA"
  686. [ "$entry_cat" != "$entry" ] && entry_catids="- [$entry_cat]"
  687. entry_date=${entry%%.$NB_DATATYPE}; entry_date=${entry_date//\_/:}
  688. NB_EntryDate=${entry_date//[A-Z]/ }
  689. echo " $list_entryid, $NB_EntryTitle - ($NB_EntryDate) $entry_catids"
  690. let id=${id}+1
  691. entry_cat=; entry_catids=; entry=
  692. done
  693. }
  694. # lists categories
  695. case "$db_query" in
  696. tag|tag[a-z]|cat) [ -z "${db_categories[*]}" ] && die "$listweblog_nocat"
  697. nb_msg "$header_ID, $header_Title"
  698. query_db; id=0
  699. cat_items=${#db_categories[*]}
  700. while [ "$id" -le "$cat_items" ]; do
  701. let id=${id}+1
  702. [ -f "$NB_DATA_DIR/cat_$id.$NB_DBTYPE" ] &&
  703. echo " $id, `nb_print "$NB_DATA_DIR"/cat_$id.$NB_DBTYPE 1`"
  704. done; exit;;
  705. esac
  706. list_entries(){
  707. [ -z "$db_query" ] && db_query="$QUERY_MODE"
  708. list_query="$db_query"
  709. # raw_db necessary for including category info
  710. raw_db "$db_query" "$db_catquery"
  711. [ -z "${DB_RESULTS[*]}" ] && die "'$list_query' - $listweblog_nomatch"
  712. }
  713. # lists entries
  714. case "$db_query" in
  715. a|any|all|entry|entries)
  716. db_query=all; list_entries
  717. nb_msg "$header_ID, $header_Title - ($header_Date) - [$header_Category]"
  718. explode_list "${DB_RESULTS[*]}";;
  719. ""|main|max|[0-9]*)
  720. list_entries
  721. nb_msg "$header_ID, $header_Title - ($header_Date) - [$header_Category]"
  722. explode_list "${DB_RESULTS[*]}";;
  723. *) die "$main_badopts $listweblog_type";;
  724. esac
  725. }
  726. # create a new entry, category or weblog directory
  727. add_weblog(){
  728. process_entry(){
  729. if [ -z "$USR_ADDFILE" ]; then
  730. # read user specified attributes for entry (interactively)
  731. if [ -z "$USR_AUTHOR" ] && [ "$BLOG_INTERACTIVE" = 1 ]; then
  732. echo "$addweblog_author [$BLOG_AUTHOR]"
  733. read -p "$NB_PROMPT" NB_EntryAuthor
  734. fi
  735. if [ -z "$USR_TITLE" ] && [ "$BLOG_INTERACTIVE" = 1 ]; then
  736. echo "$addweblog_title"
  737. read -p "$NB_PROMPT" NB_EntryTitle
  738. fi
  739. [ ! -z "$USR_TEXT" ] && NB_MetaBody="$USR_TEXT"; USR_TEXT=
  740. [ ! -z "$USR_DESC" ] && NB_EntryDescription="$USR_DESC"
  741. # prompt for description interactively
  742. if [ -z "$NB_EntryDescription" ] && [ "$DESC_PROMPT" != 1 ] &&
  743. [ "$BLOG_INTERACTIVE" = 1 ]; then
  744. echo "$addweblog_desc"
  745. read -p "$NB_PROMPT" NB_EntryDescription
  746. fi
  747. fi
  748. [ ! -z "$USR_TITLE" ] && NB_EntryTitle="$USR_TITLE"; USR_TITLE=
  749. [ ! -z "$USR_AUTHOR" ] && NB_EntryAuthor="$USR_AUTHOR"
  750. [ -z "$NB_EntryAuthor" ] && NB_EntryAuthor="$BLOG_AUTHOR"
  751. [ -z "$NB_EntryFormat" ] && NB_EntryFormat="$ENTRY_FORMAT"
  752. # set a default entry format
  753. [ -z "$NB_EntryFormat" ] && NB_EntryFormat="$ENTRY_FORMAT"
  754. # create new temp file for prepping the entry's data file
  755. NB_EditFile="$BLOG_DIR/nb_edit-entry-`nb_timestamp`.$NB_DATATYPE"
  756. # save current metadata to temp file
  757. write_entry "$NB_EditFile"
  758. # save user metadata and preserve structure from imported file
  759. if [ -f "$IMPORT_FILE" ]; then
  760. write_var "$USR_METAVAR" "$USR_SETVAR" "$IMPORT_FILE"
  761. cat "$IMPORT_FILE" > "$NB_EditFile"
  762. fi
  763. if [ -z "$NB_MetaBody" ]; then
  764. nb_msg "$addweblog_editnew"
  765. nb_edit -p "$NB_EditFile"
  766. # preserve custom meta variable and data
  767. [ ! -z "$USR_METAVAR" ] &&
  768. read_metadata "$USR_METAVAR" "$NB_EditFile"; USR_SETVAR="$METADATA"
  769. write_var "$USR_METAVAR" "$USR_SETVAR" "$NB_EditFile"
  770. # validate metafile
  771. check_metavars "TITLE: AUTHOR: DATE: BODY: $METADATA_CLOSEVAR" \
  772. "$NB_EditFile"
  773. fi
  774. }
  775. # process new
  776. process_new(){
  777. add_entry
  778. build_weblog
  779. # remove editing session file upon success
  780. [ $? = 0 ] && [ -f "$NB_EditFile" ] && rm -fr "$NB_EditFile"
  781. # prompt for some post-tasks
  782. [ ! -z "$BLOG_PREVIEW_CMD" ] && preview_weblog
  783. [ ! -z "$BLOG_PUBLISH_CMD" ] && publish_weblog
  784. }
  785. load_config
  786. case "$addweblog_type" in
  787. article)
  788. check_config
  789. # verify articles directories
  790. for articlesection in ${ARTICLES_DIR[@]}; do
  791. [ ! -d "$BLOG_DIR/$articlesection" ] &&
  792. die "$addweblog_noarticledir"
  793. done
  794. USR_ARTICLEFILE="$USR_ADDFILE"; NB_ArticleFile=
  795. if [ -z "$USR_ARTICLEFILE" ]; then
  796. NB_ArticleFile="$BLOG_DIR/nb_edit-article-`nb_timestamp`.$ARTICLES_SUFFIX"
  797. USR_ARTICLEFILE="$NB_ArticleFile"
  798. fi
  799. [ ! -z "$USR_TITLE" ] && NB_MetaTitle="$USR_TITLE"
  800. # determine the articles destination
  801. article_dirname=`dirname "$USR_ARTICLEFILE"`; article_dir=${ARTICLES_DIR%%[ ]*}
  802. article_dirbase=${article_dirname//$BLOG_DIR}; article_dirbase=${article_dirbase//$article_dir}
  803. article_dirbase=/${article_dirbase##*\/}; article_dirbase=${article_dirbase%%\/}
  804. [ "${article_dirname//$BLOG_DIR}" = "$article_dirname" ] && article_dirbase=
  805. article_filename=`chg_suffix "$article_filename" "$ARTICLES_SUFFIX"`
  806. if [ -d "$article_dirname" ] && [ ! -f "$USR_ARTICLEFILE" ]; then
  807. [ ! -z "$NB_ArticleFile" ] && make_file "$NB_ArticleFile" "$USR_TEMPLATE"
  808. article_created=1; nb_draft -p "$USR_ARTICLEFILE"
  809. fi
  810. [ ! -f "$USR_ARTICLEFILE" ] && die "$addweblog_noarticlesrc"
  811. check_metavars "TITLE: BODY: $METADATA_CLOSEVAR" "$USR_ARTICLEFILE"
  812. if [ ! -z "$NB_ArticleFile" ]; then
  813. read_metadata TITLE "$USR_ARTICLEFILE"
  814. if [ ! -z "$METADATA" ]; then
  815. article_filename=`translit_text "$METADATA"`.$ARTICLES_SUFFIX
  816. else
  817. article_filename=`translit_text "$notitle"`.$ARTICLES_SUFFIX
  818. fi
  819. else
  820. article_filename=`basename "$USR_ARTICLEFILE"`
  821. fi
  822. if [ "$article_created" != 1 ]; then
  823. [ -f "$BLOG_DIR/$article_dir${article_dirbase}/$article_filename" ] &&
  824. die "'$BLOG_DIR/$article_dir${article_dirbase}/$article_filename' - $samefilename"
  825. fi
  826. if [ ! -z "$article_dir" ] && [ -d "$BLOG_DIR/$article_dir${article_dirbase}" ]; then
  827. article_internal=${article_dirname//*${article_dir}*/true}
  828. if [ ! -z "$article_dirbase" ] || [ "$article_internal" != "true" ]; then
  829. if [ ! -f "$BLOG_DIR/$article_dir${article_dirbase}/$article_filename" ]; then
  830. cp "$USR_ARTICLEFILE" \
  831. "$BLOG_DIR/$article_dir${article_dirbase}/$article_filename" || exit
  832. article_copied=1
  833. fi
  834. fi
  835. [ -f "$NB_ArticleFile" ] && rm -f "$NB_ArticleFile"
  836. if [ "$article_created" = 1 ] || [ "$article_copied" = 1 ]; then
  837. nb_msg "$addweblog_newarticle"
  838. NB_QUERY=main; build_weblog
  839. # prompt for some post-tasks
  840. [ ! -z "$BLOG_PREVIEW_CMD" ] && preview_weblog
  841. [ ! -z "$BLOG_PUBLISH_CMD" ] && publish_weblog
  842. exit
  843. fi
  844. fi
  845. die "$addweblog_noarticlesrc"
  846. ;;
  847. entry)
  848. check_config
  849. # add new entry from metafile
  850. [ ! -z "$USR_ADDFILE" ] && import_file "$USR_ADDFILE"
  851. process_entry; process_new
  852. ;;
  853. weblog)
  854. [ -z "$BLOG_DIR" ] && die "$addweblog_noweblog"
  855. [ -d "$BLOG_DIR" ] && die "$addweblog_nocreate"
  856. # create new weblog directory ...
  857. if [ ! -d "$BLOG_DIR" ]; then
  858. nb_msg "$addweblog_newblog '$BLOG_DIR' ..."
  859. mkdir -p "$BLOG_DIR"
  860. [ ! -d "$BLOG_DIR" ] &&
  861. die "$addweblog_nocreate"
  862. > "$BLOG_DIR/.nb_newblogdir"
  863. nb_msg "$addweblog_copyaction"
  864. # copy default files and directories
  865. for weblog_dir in "$NB_BASE_DIR"/default/*; do
  866. cp -R "$weblog_dir" "$BLOG_DIR"
  867. done
  868. # create some critical empty directories
  869. for weblog_emptydir in "$ARCHIVES_DIR" "$CACHE_DIR" "$PARTS_DIR"; do
  870. [ ! -d "$BLOG_DIR/$weblog_emptydir" ] && mkdir "$BLOG_DIR/$weblog_emptydir"
  871. done
  872. # ... prompt for configuration, unless interactive mode is 0
  873. if [ "$BLOG_INTERACTIVE" = 0 ]; then
  874. choice=n
  875. else
  876. echo "$addweblog_askconf [Y/n]"
  877. read -p "$NB_PROMPT" choice
  878. fi
  879. case $choice in
  880. [Yy]|"")
  881. nb_msg "$addweblog_confaction"
  882. nb_edit "$BLOG_DIR"/blog.conf
  883. check_config; build_weblog
  884. exit;;
  885. [Nn])
  886. check_config; build_weblog
  887. nb_msg "$addweblog_noconf"
  888. exit;;
  889. esac
  890. fi
  891. ;;
  892. tag|cat)
  893. check_config
  894. # create a new category
  895. query_db
  896. id=0; cat_items=${#db_categories[*]}
  897. while [ "$id" -lt "$cat_items" ] || [ "$cat_items" = 0 ]; do
  898. let id=${id}+1
  899. if [ ! -f "$NB_DATA_DIR/cat_$id.$NB_DBTYPE" ]; then
  900. nb_msg "$addweblog_newcat id: $id ..."
  901. if [ ! -z "$USR_TITLE" ]; then
  902. cat_title=$USR_TITLE; USR_TITLE=
  903. else
  904. echo "$addweblog_titlecat [$addweblog_untitled]"
  905. read -p "$NB_PROMPT" cat_title
  906. [ -z "$cat_title" ] && cat_title="$notitle"
  907. fi
  908. translit_text "$cat_title" > "$NB_DATA_DIR"/cat_$id.$NB_DBTYPE
  909. cat_num="$id"; db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  910. cat_items="$id"
  911. else
  912. let cat_items=${cat_items}+1
  913. db_categories=(${db_categories[*]} cat_$id.$NB_DBTYPE)
  914. fi
  915. done
  916. newcat_title=`nb_print "$NB_DATA_DIR"/cat_$id.$NB_DBTYPE`
  917. nb_msg "$addweblog_madecat '$newcat_title'."; exit
  918. ;;
  919. *)
  920. die "$main_badopts $addweblog_type"
  921. ;;
  922. esac
  923. }
  924. show_help(){
  925. CURRENT_YEAR="2011"
  926. COPYRIGHT="2004-$CURRENT_YEAR"
  927. NB_ShowHelp=$(< "${NB_LANG_DIR}/$NB_LANG"/help.txt)
  928. BASENAME=`basename $0`
  929. # transform keywords to variables and delete all hash-marked lines for display
  930. sed -e "/[\$]VERSION/ s//${VERSION}/g; /[\$]BASENAME/ s//${BASENAME}/g; /[\$]COPYRIGHT/ s//${COPYRIGHT}/g; /^[\#]/d" <<-EOF
  931. $NB_ShowHelp
  932. EOF
  933. }
  934. # load minimal configuration data required to operate
  935. load_globals
  936. load_lang
  937. [ $# -lt 1 ] && show_help
  938. check_arg(){
  939. if [ -z "$1" ]; then
  940. echo "$bad_argument $checkarg_badarg"
  941. echo "$checkarg_help"
  942. exit 1
  943. fi
  944. }
  945. sanity_check(){
  946. invalid_opt=`echo "$@" |grep '^[--]$*'`
  947. [ ! -z "$invalid_opt" ] && argument=
  948. }
  949. while [ $# -gt 0 ]; do
  950. bad_argument=$1
  951. # -a,-c,-d,-E,-e,-l,-m,-q,-u exists for backwards compability
  952. case "$1" in
  953. add|-a) check_arg "$2"; addweblog_type="$2"; shift; ADD_WEBLOG=1;;
  954. configure) CONFIG_WEBLOG=1;;
  955. edit) check_arg "$2"; editweblog_type="$2"; edit_idsel="$3"; shift 2
  956. EDIT_WEBLOG=1;;
  957. -e) check_arg "$2"; editweblog_type=entry; edit_idsel="$2"; shift
  958. EDIT_WEBLOG=1;; # short option for backward compatibility
  959. delete) check_arg "$2"; delweblog_type="$2"
  960. case "$3" in [0-9]*) delete_idsel="$3"; shift 2;; *) shift;;
  961. esac; DELETE_WEBLOG=1;;
  962. -d) check_arg "$2"; delweblog_type=entry; delete_idsel="$2"; shift
  963. DELETE_WEBLOG=1;; # short option for backward compatibility
  964. draft|-E) check_arg "$2"; USR_DRAFTFILE="$2"; shift; DRAFT_WEBLOG=1;;
  965. import) check_arg "$2"; addweblog_type="$2"; USR_ADDFILE="$3"; shift 2
  966. ADD_WEBLOG=1;;
  967. list|-l) listweblog_type="$2"
  968. case "$3" in [0-9]*) list_idsel="$3"; shift 2;; *) shift;;
  969. esac; LIST_WEBLOG=1;;
  970. make-page) check_arg "$2"; USR_MKPSRCFILE="$2"; USR_MKPOUTFILE="$3"; shift 2
  971. MAKEPAGE_WEBLOG=1;;
  972. make-file) check_arg "$2"; USR_MKFFILE="$2"; shift; MAKEFILE_WEBLOG=1;;
  973. manual) load_config; load_lang; nb_browser "$NB_DOC_FILE"; exit;;
  974. tag-entry|-m) check_arg "$2"; catentry_num="$2"; shift; MOVE_WEBLOG=1;;
  975. publish) PUBLISH_WEBLOG=1;;
  976. preview) PREVIEW_WEBLOG=1;;
  977. update|-u) updweblog_type="$2";
  978. case "$3" in [0-9]*) update_idsel="$3"; shift 2;; *) shift;;
  979. esac; UPDATE_WEBLOG=1;;
  980. update-cache) updcache_type="$2"
  981. case "$3" in [0-9]*) updatec_idsel="$3"; shift 2;; *) shift;;
  982. esac; UPDATECACHE_WEBLOG=1;;
  983. --author) check_arg "$2"; USR_AUTHOR="$2"; shift;;
  984. --conf-file) check_arg "$2"; USR_BLOGCONF="$2"; shift;;
  985. --data-dir) check_arg "$2"; USR_DATADIR="$2"; shift;;
  986. --desc) DESC_PROMPT=1; USR_DESC="$2"; shift;;
  987. --meta-file) check_arg "$2"; USR_ADDFILE="$2"; shift;;
  988. --title) check_arg "$2"; USR_TITLE="$2"; shift;;
  989. --plugin-dir) check_arg "$2"; USR_PLUGINSDIR="$2"; shift;;
  990. --query|-q) check_arg "$2"; NB_QUERY="$2"; shift; QUERY_WEBLOG=1;;
  991. --set-var) check_arg "$2"; USR_SETVAR="$2"; shift;;
  992. --tag|-c) check_arg "$2"; cat_num="$2"; shift;;
  993. --text) check_arg "$2"; USR_TEXT="$2"; shift;;
  994. --template) check_arg "$2"; USR_TEMPLATE="$2"; shift;;
  995. --template-dir) check_arg "$2"; USR_TEMPLATE_DIR="$2"; shift;;
  996. --var) check_arg "$2"; USR_METAVAR="$2"; shift;;
  997. --version) echo "NanoBlogger $VERSION"; exit;;
  998. --no-preview) NOPREVIEW_WEBLOG=1;;
  999. --no-publish) NOPUBLISH_WEBLOG=1;;
  1000. -b|--blog-dir) check_arg "$2"; USR_BLOGDIR="$2"; shift;;
  1001. -f|--force) FORCE_UPDATE=1; UPDATECACHE_WEBLOG=1;;
  1002. -h|--help|help) show_help; exit;;
  1003. -i|--interactive) check_arg "$2"; USR_INTERACTIVE="$2"; shift;;
  1004. -v|--verbose) check_arg "$2"; VERBOSE="$2"; shift;;
  1005. --) shift; break;;
  1006. *)
  1007. sanity_check
  1008. echo "$main_badopts $bad_argument"; echo "$main_panic"
  1009. exit 1
  1010. ;;
  1011. esac
  1012. shift
  1013. done
  1014. # post-processing for all command line args
  1015. if [ "$CONFIG_WEBLOG" = 1 ]; then
  1016. check_config; config_weblog
  1017. [ "$RESORT_DATABASE" = 1 ] && resort_database
  1018. fi
  1019. if [ "$ADD_WEBLOG" = 1 ]; then
  1020. add_weblog
  1021. fi
  1022. if [ "$DELETE_WEBLOG" = 1 ]; then
  1023. check_config; delete_weblog
  1024. fi
  1025. if [ "$DRAFT_WEBLOG" = 1 ]; then
  1026. check_config; nb_draft
  1027. fi
  1028. if [ "$EDIT_WEBLOG" = 1 ]; then
  1029. check_config; edit_weblog
  1030. fi
  1031. if [ "$LIST_WEBLOG" = 1 ]; then
  1032. check_config; list_weblog
  1033. fi
  1034. if [ "$MAKEPAGE_WEBLOG" = 1 ]; then
  1035. check_config; weblog_page "$USR_MKPSRCFILE" "$USR_TEMPLATE" \
  1036. "$USR_MKPOUTFILE"
  1037. fi
  1038. if [ "$MAKEFILE_WEBLOG" = 1 ]; then
  1039. check_config
  1040. # don't overwrite existing files
  1041. [ -f "$USR_MKFFILE" ] &&
  1042. die "'$USR_MKFFILE' - $samefilename"
  1043. make_file "$USR_MKFFILE" "$USR_TEMPLATE"
  1044. fi
  1045. if [ "$MOVE_WEBLOG" = 1 ]; then
  1046. check_config; cat_entry
  1047. fi
  1048. if [ "$UPDATECACHE_WEBLOG" = 1 ]; then
  1049. USR_CACHEQUERY="$NB_QUERY"
  1050. check_config; db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  1051. update_cache "$USR_CACHEQUERY" "*"
  1052. fi
  1053. if [ "$UPDATE_WEBLOG" = 1 ]; then
  1054. check_config; db_catquery=`cat_id "$cat_num"`; check_catid "$cat_num"
  1055. build_weblog
  1056. if [ "$PREVIEW_WEBLOG" = 1 ]; then
  1057. check_config; preview_weblog
  1058. fi
  1059. if [ "$PUBLISH_WEBLOG" = 1 ]; then
  1060. check_config; publish_weblog
  1061. fi
  1062. else
  1063. if [ "$PREVIEW_WEBLOG" = 1 ]; then
  1064. check_config; preview_weblog
  1065. fi
  1066. if [ "$PUBLISH_WEBLOG" = 1 ]; then
  1067. check_config; publish_weblog
  1068. fi
  1069. fi
  1070. exit 0