File: hylang

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (72 lines) | stat: -rw-r--r-- 2,189 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env hy

(import hy.core)


(defn hyghlight-names []
  (-> (hy.core.reserved.names)
      (sorted)))


(defn hyghlight-keywords []
  (sorted hy.core.reserved.keyword.kwlist))


(defn hyghlight-builtins []
  (sorted
   (- (hy.core.reserved.names)
      (frozenset hy.core.reserved.keyword.kwlist))))


(defmacro replace-line [spaces line-format end-of-line keywords]
  `(+ line
      (.join ~end-of-line
             (list-comp
              (.format ~line-format
                       :space (* " " ~spaces)
                       :line (.join " " keyword-line))
              [keyword-line (partition ~keywords 10)]))
      "\n"))

(defn generate-highlight-js-file []
  (defn replace-highlight-js-keywords [line]
    (cond
     [(in "// keywords" line) (replace-line 6
                                            "{space}'{line} '"
                                            " +\n"
                                            (hyghlight-names))]
     [True line]))

  (with [f (open "templates/highlight_js/hy.js")]
        (.join ""
               (list-comp (replace-highlight-js-keywords line)
                          [line (.readlines f)]))))


(defn generate-rouge-file []
  (defn replace-rouge-keywords [line]
    (cond
     [(in "@keywords" line) (replace-line 10
                                          "{space}{line}"
                                          "\n"
                                          (hyghlight-keywords))]
     [(in "@builtins" line) (replace-line 10
                                          "{space}{line}"
                                          "\n"
                                          (hyghlight-builtins))]
     [True line]))

  (with [f (open "templates/rouge/hylang.rb")]
        (.join ""
               (list-comp (replace-rouge-keywords line)
                          [line (.readlines f)]))))


(defmain [&rest args]
  (let [highlight-library (second args)]
    (print (cond
            [(= highlight-library "highlight.js")
             (generate-highlight-js-file)]
            [(= highlight-library "rouge")
             (generate-rouge-file)]
            [True "Usage: hy hyghlight.hy [highlight.js | rouge]"]))))