Permalink
Browse files

Skeletonization.

  • Loading branch information...
Avdi Grimm
Avdi Grimm committed Mar 4, 2012
1 parent c1a24eb commit aaf93540b81b062f4a1bca77904ade8707a6aa0a
Showing with 163 additions and 19 deletions.
  1. +43 −0 init.m4
  2. +49 −16 orgpress.mk
  3. +3 −3 skeleton.org.m4
  4. +57 −0 skeletonize.awk
  5. +11 −0 styles.css
View
43 init.m4
@@ -0,0 +1,43 @@
+m4_changequote(`[[[',`]]]')m4_dnl
+m4_changecom([[[#-]]])m4_dnl
+
+#- Ignore whitespace in macro defs
+m4_divert([[[-1]]])
+
+#- Get the listing filename for a given listing name
+#- ORGPRESS_LISTING_FILE(listing_name)
+m4_define([[[ORGPRESS_LISTING_FILE]]],[[[ORGPRESS_listings_dir/$1.listing]]])
+
+#- Output a fancy code listing with a caption
+#- ORGPRESS_FANCY_LISTING(listing_name, caption)
+m4_define([[[ORGPRESS_FANCY_LISTING]]],
+[[[#+HTML: <div class="listing">
+#+LaTeX: \begin{listing}[H]
+#+LaTeX: \caption{$2}
+m4_include(ORGPRESS_LISTING_FILE($1))m4_dnl
+#+LaTeX: \end{listing}
+#+HTML: <div class="caption">$2</div>
+#+HTML: </div>]]])m4_dnl
+
+#- Output a fancy figure with caption
+#- ORGPRESS_FANCY_FIGURE(filename, caption)
+m4_define([[[ORGPRESS_FANCY_FIGURE]]],
+[[[#+CAPTION: $2
+[[$1]] ]]])
+
+#- Output a code listing. When caption is provided, output a fancy listing.
+#- ORGPRESS_LISTING(name [, caption])
+m4_define([[[ORGPRESS_LISTING]]],
+ [[[m4_ifelse($2,,
+ [[[m4_include(ORGPRESS_LISTING_FILE($1))]]],
+ [[[ORGPRESS_FANCY_LISTING($1,$2)]]])]]])
+
+#- Output a figure, optionally with a caption
+#- ORGPRESS_FIGURE(filename[, caption])
+m4_define([[[ORGPRESS_FIGURE]]],
+ [[[m4_ifelse($2,,
+ [[[ [[$1]] ]]],
+ [[[ORGPRESS_FANCY_FIGURE($1,$2)]]])]]])
+
+#- OK, done defining macros
+m4_divert
View
@@ -30,6 +30,11 @@ EMACSFLAGS =
# Command for viewing files
OPEN = xdg-open
+# Macro processor
+M4 = m4
+M4_INIT_FILE = $(abspath $(ORGPRESS_ROOT)/init.m4)
+M4FLAGS = --prefix-builtins $(M4_INIT_FILE)
+
ifdef DEBUG
EMACSFLAGS += --debug-init
endif
@@ -67,13 +72,22 @@ endef
# The input HTML file for Calibre conversions
CALIBRE_INPUT = $(call flavor_file,calibre.html)
+# Awk! Awk! Awk!
+AWK = gawk
+AWKFLAGS =
+
+# Strip out figures and listings, replace with placeholders
+skeletonize_file = $(realpath $(ORGPRESS_ROOT)/skeletonize.awk)
+skel = $(AWK) $(AWKFLAGS) -f $(skeletonize_file)
+skelflags = $(foreach var,listings_dir figures_dir,-v$(var)="$($(var))")
+
### BOOK METADATA ###
# The basename of the book, for use in filenames
BOOK_NAME = $(notdir $(CURDIR))
-# The main org-mode source file
-SOURCE_FILE = $(BOOK_NAME).org
+# The main org-mode source files
+SOURCE_FILES = $(realpath $(BOOK_NAME).org)
# The book title
BOOK_TITLE = $(BOOK_NAME)
@@ -128,12 +142,17 @@ BUNDLE_FILES = $(BUNDLE_FLAVORS:%=$(BUILD_DIR)/$(BOOK_NAME).%)
# Standard dependencies
STANDARD_DEPS = $(ORGPRESS_MAKEFILE) $(BOOK_MAKEFILE) $(build_assets)
-export_target := $(BUILD_DIR)/$(BOOK_NAME).$(FLAVOR)
-skeleton := $(BUILD_DIR)/$(BOOK_NAME).org
-skeleton_vars := BOOK_TITLE AUTHORS SOURCE_FILE
-define skeleton_defs
-$(strip $(foreach varname,$(skeleton_vars),-D ORGPRESS_$(varname)="$($(varname))"))
+all_targets = $(addprefix $(BOOK_NAME).,$(ALL_FLAVORS))
+export_target = $(BUILD_DIR)/$(BOOK_NAME).$(FLAVOR)
+master = $(BUILD_DIR)/master.org
+master_vars = BOOK_TITLE AUTHORS SOURCE_FILES sections_file listings_dir figures_dir
+define master_defs
+$(strip $(foreach varname,$(master_vars),-D ORGPRESS_$(varname)="$($(varname))"))
endef
+sections_file = $(BUILD_DIR)/sections.org.m4
+skeletons = $(SOURCE_FILES:$(CURDIR)/%.org=$(BUILD_DIR)/%.skeleton)
+listings_dir = $(BUILD_DIR)/listings
+figures_dir = $(BUILD_DIR)/figures_dir
### FUNCTIONS ###
@@ -168,7 +187,7 @@ $(info OrgPress version $(ORGPRESS_VERSION))
ifdef DEBUG
$(info Debug mode enabled)
- DEBUG_PRECIOUS_FILES = $(skeleton)
+ DEBUG_PRECIOUS_FILES = $(master)
endif
include $(BOOK_MAKEFILE)
@@ -189,31 +208,45 @@ $(ALL_FLAVORS):
$(MAKE) $(call flavor_file,$@)
if [ -n "$(OPEN_TARGET)" ]; then $(OPEN) $(call flavor_file,$@); fi
-%.pdf %.html %.txt %.mobi %.epub: FLAVOR = $(subst .,,$(suffix $@))
+$(addprefix %.,$(ALL_FLAVORS)): FLAVOR = $(subst .,,$(suffix $@))
$(CALIBRE_TARGETS): flavorflags = $($(call uppercase,$(FLAVOR))FLAGS)
$(CALIBRE_TARGETS): $(CALIBRE_INPUT) $(CURDIR)/book.mk $(STANDARD_DEPS)
$(CONVERT) $< $@ $(strip $(CONVERTFLAGS)) $(strip $(flavorflags))
-%.pdf %.html %.txt: %.org $(EMACS_LOAD) $(STANDARD_DEPS)
+%.pdf %.html %.calibre.html %.txt: %.org $(EMACS_LOAD) $(STANDARD_DEPS) $(STYLESHEET)
$(EMACS) $(EMACSFLAGS) \
$(EMACS_LOAD:%=-l %) \
--user $(USER) \
--batch \
--file "$<" \
--eval '$(strip $(export_elisp))'
-$(BUILD_DIR):
+$(BUILD_DIR)/$(BOOK_NAME).org: $(master)
+ cp $< $@
+
+$(BUILD_DIR) $(listings_dir) $(figures_dir):
mkdir -p $@
-skeleton: $(skeleton)
+master: $(master)
-$(skeleton): | $(BUILD_DIR)
-$(skeleton): $(ORGPRESS_ROOT)/skeleton.org.m4 $(CURDIR)/book.mk $(ORGPRESS_MAKEFILE)
- m4 $(skeleton_defs) $< > $@
+# Order-only prerequisite on build dir
+$(master): | $(BUILD_DIR)
+$(master): $(ORGPRESS_ROOT)/skeleton.org.m4 $(CURDIR)/book.mk $(ORGPRESS_MAKEFILE) $(sections_file) $(M4_INIT_FILE)
+ $(M4) $(M4FLAGS) $(master_defs) $< > $@
+
+$(sections_file): $(STANDARD_DEPS) $(skeletons)
+ -rm $@
+ for fn in $(skeletons); do echo "m4_include($${fn})" >> $@; done
$(build_assets):
mkdir -p $(@D)
cp -r $(@:$(BUILD_DIR)/%=%) $@
-.PRECIOUS: $(DEBUG_PRECIOUS_FILES)
+$(BUILD_DIR)/%.skeleton: $(CURDIR)/%.org $(listings_dir) $(figures_dir) $(skeletonize_file) $(STANDARD_DEPS)
+ $(skel) $(skelflags) $< > $@
+
+.PRECIOUS: $(DEBUG_PRECIOUS_FILES)
+
+# Targets that aren't files and never will be
+.PHONY: $(ALL_FLAVORS) default info master clean
View
@@ -1,4 +1,4 @@
-`#'+TITLE: ORGPRESS_BOOK_TITLE
-`#'+AUTHOR: ORGPRESS_AUTHORS
+#+TITLE: ORGPRESS_BOOK_TITLE
+#+AUTHOR: ORGPRESS_AUTHORS
-`#'+INCLUDE ORGPRESS_SOURCE_FILE
+m4_include(ORGPRESS_sections_file)
View
@@ -0,0 +1,57 @@
+# "Skeletonize" a file by pulling out code listings and possibly other objects
+# into separate files, replacing them with placeholders in the source text.
+#
+# Required variables (these must be be set at the command line with -vkey=value)
+#
+# - listings_dir
+# - figures_dir
+#
+BEGIN {
+ # Ignore case in all regexps
+ IGNORECASE = 1
+ caption_pattern = /^[[:space:]]*#\+CAPTION:/
+}
+
+function start_listing()
+{
+ ++listing_count
+ ("basename " FILENAME " .org") | getline basename
+ formatted_count = sprintf("%03d", listing_count)
+ listing_name = (basename "-" formatted_count)
+ listing_file = (listings_dir "/" listing_name ".listing")
+ system("rm " listing_file "> /dev/null 2>&1")
+ if(caption)
+ print "ORGPRESS_LISTING(" listing_name ",[[[" caption "]]])"
+ else
+ print "ORGPRESS_LISTING(" listing_name ")"
+ caption = ""
+}
+
+/^[[:space:]]*#\+BEGIN_SRC/ { start_listing() }
+/^[[:space:]]*#\+BEGIN_EXAMPLE/ { start_listing() }
+/^[[:space:]]*#\+BEGIN_SRC/,/^[[:space:]]*#\+END_SRC/ {
+ print >listing_file
+ next
+}
+/^[[:space:]]*#\+BEGIN_EXAMPLE/,/^[[:space:]]*#\+END_EXAMPLE/ {
+ print >listing_file
+ next
+}
+# Inline image links
+/^[[:space:]]*\[\[.*\.(png|jpeg|eps|jpg)\]\][[:space:]]*/ {
+ match($0, /\[\[(.*)]\]/, matches)
+ figure_file = matches[1]
+ if(caption)
+ print "ORGPRESS_FIGURE(" figure_file ",[[[" caption "]]])"
+ else
+ print "ORGPRESS_FIGURE(" figure_file ")"
+ caption = ""
+ next
+}
+/^[[:space:]]*#\+CAPTION:/ {
+ caption = $0
+ sub(/^[[:space:]]*#\+CAPTION:[[:space:]]*/,"",caption)
+ next
+}
+{ print }
+
View
@@ -1048,3 +1048,14 @@
/* widget-single-line-field */
background-color: #d9d9d9;
}
+
+.listing .caption, .figure p:nth-child(2) {
+ font-size: smaller;
+ font-style: italic;
+ text-align: center;
+}
+
+pre {
+ margin-left: 3ex;
+ margin-right: 3ex;
+}

0 comments on commit aaf9354

Please sign in to comment.