Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit da657c2b9a28037089e97da668016c97241d7dc9 0 parents
Stathis Sideris authored
5 .gitignore
... ... @@ -0,0 +1,5 @@
  1 +pom.xml
  2 +*jar
  3 +/lib/
  4 +/classes/
  5 +.lein-deps-sum
13 README
... ... @@ -0,0 +1,13 @@
  1 +# javamatic
  2 +
  3 +FIXME: write description
  4 +
  5 +## Usage
  6 +
  7 +FIXME: write
  8 +
  9 +## License
  10 +
  11 +Copyright (C) 2011 FIXME
  12 +
  13 +Distributed under the Eclipse Public License, the same as Clojure.
3  project.clj
... ... @@ -0,0 +1,3 @@
  1 +(defproject javamatic "1.0.0-SNAPSHOT"
  2 + :description "FIXME: write description"
  3 + :dependencies [[org.clojure/clojure "1.2.1"]])
108 src/javamatic/core.clj
... ... @@ -0,0 +1,108 @@
  1 +(ns javamatic.core
  2 + (:require [clojure.contrib.str-utils :as str1]
  3 + [clojure.contrib.str-utils2 :as str2])
  4 + (:use [clojure.contrib.str-utils2 :only [lower-case]])
  5 + (:import [java.awt Toolkit]
  6 + [java.awt.datatransfer StringSelection]))
  7 +
  8 +(defn copy
  9 + "Copy passed string into clipboard."
  10 + [s]
  11 + (let [ss (StringSelection. s)]
  12 + (.setContents
  13 + (.getSystemClipboard (Toolkit/getDefaultToolkit)) ss ss)
  14 + s))
  15 +
  16 +(defn placeholder?
  17 + "Tests whether the passed string is a template placeholder."
  18 + [s]
  19 + (re-find #"^\{\{" s))
  20 +
  21 +(defn eval-placeholder?
  22 + "Tests whether the passed string is a template placeholder that
  23 + needs to be evaluated."
  24 + [s]
  25 + (re-find #"^\{\{" s))
  26 +
  27 +;;;;;;;;;;;
  28 +
  29 +(defn capitalize
  30 + "Capitalize the first letter of a string, but leave the rest untouched."
  31 + [s]
  32 + (str (str2/capitalize (str2/take s 1)) (str2/drop s 1)))
  33 +
  34 +(defn add-spaces
  35 + "Seperate camel-case with spaces."
  36 + [s]
  37 + (str2/trim (str2/replace s #"[A-Z]" #(str " " %))))
  38 +
  39 +;;;;;;;;;;;
  40 +
  41 +(defn first-alpha
  42 + "Get all the characters at the beggining of the string up to the
  43 + first non-alphanumeric character."
  44 + [s]
  45 + (let [m (re-find #"^\s*[A-Za-z0-9]+" s)]
  46 + (when m (str2/trim m))))
  47 +
  48 +(defn first-alphas
  49 + "Split the passed string at new lines and apply get-first-alpha to
  50 + each line."
  51 + [s]
  52 + (remove nil? (map get-first-alpha (str2/split s #"\n"))))
  53 +
  54 +(defn name-from-declaration
  55 + "Extract the variable name from a single Java declaration."
  56 + [s]
  57 + (second (remove (apply hash-set
  58 + "" (qw public protected private static volatile transient final))
  59 + (str2/split s #"\s+"))))
  60 +
  61 +(defn names-from-declarations
  62 + "Split the passed string at new lines and apply
  63 + name-from-declaration to each line."
  64 + [s]
  65 + (remove nil? (map name-from-declaration (str2/split s #"\n"))))
  66 +
  67 +;;;;;;;;;;;
  68 +
  69 +(defmacro qw
  70 + "Constructs a vector of the names (strings) of the passed symbols.
  71 + This is to save you typing unneccesary quotes. Stolen from Perl.
  72 +
  73 + Example: (qw \"first name\" surname address)"
  74 + [& words]
  75 + `(vector ~@(map name words)))
  76 +
  77 +(def x nil)
  78 +(defn eval-placeholder [placeholder value]
  79 + (let [code (read-string (str2/butlast (str2/drop placeholder 2) 2))]
  80 + (binding [x value]
  81 + (eval code))))
  82 +
  83 +(defn render-template-single [t x]
  84 + (apply str
  85 + (map #(cond (eval-placeholder? %) (eval-placeholder % x)
  86 + (placeholder? %) x
  87 + :else %)
  88 + (str1/re-partition #"\{\{.+?\}\}" t))))
  89 +
  90 +(defn render-template [t values]
  91 + (apply str
  92 + (map #(render-template-single t %) values)))
  93 +
  94 +(copy (render-template
  95 + "set{{x}}(\"{{(add-spaces x)}}\");\n"
  96 + (qw FirstName Surname Address Email)))
  97 +
  98 +
  99 +
  100 +
  101 +
  102 +
  103 +
  104 +
  105 +
  106 +
  107 +
  108 +
6 test/javamatic/test/core.clj
... ... @@ -0,0 +1,6 @@
  1 +(ns javamatic.test.core
  2 + (:use [javamatic.core])
  3 + (:use [clojure.test]))
  4 +
  5 +(deftest replace-me ;; FIXME: write
  6 + (is false "No tests have been written."))

0 comments on commit da657c2

Please sign in to comment.
Something went wrong with that request. Please try again.