Browse Source

Rewrote the org file indexer

Samuel W. Flint 8 years ago
parent
commit
1a75c18041
1 changed files with 67 additions and 12 deletions
  1. 67 12
      org-file-index

+ 67 - 12
org-file-index

@@ -1,23 +1,78 @@
-#!/bin/zsh -f
+#!/usr/bin/perl
 
-TODAY=`date +"<%Y-%m-%d %a %H:%M>"`
+use strict;
+use warnings;
+use POSIX qw/strftime/;
 
-rm ~/org/index.org
+my $outputfile = "/home/swflint/org/index.org";
+my $date = strftime "<%Y-%m-%d %a %H:%M>", localtime;
 
-cat <<EOF >> ~/org/index.org
+my $fileHeader = <<EOF;
 #+Title: Org File Index
 #+AUTHOR: Sam Flint
-#+EMAIL: swflint@flintfam.org
-#+DATE: ${TODAY}
+#+EMAIL: swflint\@flintfam.org
+#+DATE: $date
 #+INFOJS_OPT: view:info toc:nil path:http://flintfam.org/org-info.js
 #+OPTIONS: toc:nil H:5 ':t *:t d:nil stat:nil todo:nil
 #+LATEX_CLASS_OPTIONS: [10pt,twocolumn]
-#+LATEX_HEADER: \usepackage[landscape,margin=0.125 in]{geometry}
-#+LATEX_HEADER: \pagestyle{empty}
+#+LATEX_HEADER: \\usepackage[landscape,margin=0.125 in]{geometry}
+#+LATEX_HEADER: \\pagestyle{empty}
+
+* Org
 
 EOF
 
-for file in `find ~/org -name '*.org' -type f` ; do
-    TITLE=`echo ${file} | sed "s/\/home\/swflint\/org\/\\(.*\\)\.org/\1/g"`
-    echo " - [[file:${file}][${TITLE}]]" >> ~/org/index.org
-done
+open(my $output, ">", $outputfile) or die "Unable to open \"$outputfile\" the output file, $!";
+
+print $output $fileHeader;
+
+opendir(my $directory, "/home/swflint/org/")
+    or die "Can't open the directory: $!";
+
+my @directories;
+
+while (my $file = readdir $directory) {
+    if ($file =~ m/^\..*/) {
+        next;
+    } elsif (-d "/home/swflint/org/$file") {
+        push @directories, $file;
+    } elsif ($file =~ m/.*\.org$/) {
+        (my $linkName = $file) =~ s/\.[^.]+$//;
+        $linkName =~ s/-/ /g;
+        $linkName =~ s/\b(\w)/\U$1/g;
+        print $output " - [[file:~/org/$file][$linkName]]\n"
+    } else {
+        next;
+    }
+}
+
+closedir $directory;
+
+foreach (sort @directories) {
+    my $newDir = $_;
+    if ($newDir =~ "auto" or $newDir =~ "data") {
+        next;
+    }
+    opendir(my $theDirectory, "/home/swflint/org/$newDir/")
+        or die "Can't open directory: $!";
+    my $fancyDir = $newDir;
+    $fancyDir =~ s/-/ /g;
+    $fancyDir =~ s/\b(\w)/\U$1/g;
+    print $output "\n** Org: $fancyDir\n\n";
+    print $output " - [[file:~/org/$newDir/][$fancyDir]]\n";
+    while (my $file = readdir $theDirectory) {
+        if ($file =~ m/^\..*/
+            or -d "/home/swflint/org/$newDir/$file") {
+            next;
+        } elsif ($file =~ m/.*\.org$/) {
+            (my $linkName = $file) =~ s/\.[^.]+$//;
+            $linkName =~ s/-/ /g;
+            $linkName =~ s/\b(\w)/\U$1/g;
+            print $output " - [[file:~/org/$newDir/$file][$linkName]]\n";
+        } else {
+            next;
+        }
+    }
+}
+
+close $output;