Summary Files Commits Branches @commit @diff @stats
path: root / generate-hooks / create-description.awk
diff options
context:
space:
mode:
1 files changed, 27 insertions, 7 deletions
-rw-r--r--generate-hooks/create-description.awk34
diff --git a/generate-hooks/create-description.awk b/generate-hooks/create-description.awk
index b4fac0e..ec0bace 100644
--- a/generate-hooks/create-description.awk
+++ b/generate-hooks/create-description.awk
@@ -1,18 +1,38 @@
-/p>.*/ {
+# For every page, the description meta tag is "[DESCRIPTION]", with this we
+# generate it. At most, the first 200 characters from all paragraphs are taken.
+
+function appToDesc(l) {
# Removes tags and bad characters
- gsub(/<[^>]*>/, "") ; gsub(/[^a-zA-Z0-9 ]/, "")
+ gsub(/<[^>]*>/, "", l) ; gsub("&#8217;", "'", l) ; gsub(/[^a-zA-Z0-9 '.]/, "", l)
# Trims leading and trailing spaces and tabs (if any)
- gsub(/^[ \t]*/, "") ; gsub(/[ \t]*$/, "")
+ gsub(/^[ \t]*/, "", l) ; gsub(/[ \t]*$/, "", l)
# Concatenates the current record to variable and shortens it,
# when it gets too long
- desc = desc $0
+ desc = desc l
if (length(desc) >= 200) {
desc = substr(desc, 1, 197) "..."
- exit
+ return 1
}
+ return 0
+}
+
+BEGIN {
+ stop = 0
+
+ # Loop over the whole file, extract all text in first couple of paragraphs,
+ # store it in desc (but no more than 200 characters will be saved)
+ while(stop == 0 && getline < ARGV[1]) {
+ if (index($0, "<p") > 0)
+ stop = appToDesc($0)
+ }
+ close(ARGV[1]) # FILENAME isn't set during BEGIN
+}
+
+"\\[DESCRIPTION\\]" {
+ gsub("\\[DESCRIPTION\\]", desc)
}
-END {
- print desc
+{
+ print
}