File: lasso.rake

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 (74 lines) | stat: -rw-r--r-- 2,283 bytes parent folder | download | duplicates (2)
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
73
74
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

require 'open-uri'

LASSO_SYNTAX_URI = "https://raw.githubusercontent.com/LassoSoft/Lasso-HTML.mode/master/Contents/Resources/SyntaxDefinition.xml"
LASSO_KEYWORDS_FILE = "./lib/rouge/lexers/lasso/keywords.rb"

namespace :builtins do
  task :lasso do
    generator = Rouge::Tasks::Builtins::Lasso.new

    input    = URI.open(LASSO_SYNTAX_URI) { |f| f.read }
    keywords = generator.extract_keywords(input)

    output = generator.render_output(keywords)

    File.write(LASSO_KEYWORDS_FILE, output)
  end
end

module Rouge
  module Tasks
    module Builtins
      class Lasso
        def extract_keywords(input)
          groups = {"Types" => :types,
                    "Traits" => :traits,
                    "Lasso 8 Tags" => :builtins,
                    "Unbound Methods" => :builtins,
                    "Keywords" => :keywords,
                    "Error Keywords" => :exceptions}

          keywords = Hash.new { |h,k| h[k] = Array.new }

          input.scan(%r[<keywords id="(.*?)" .*?>(.*?)</keywords>]m) do |m|
            next unless groups.keys.include? m[0]

            values = m[1].split(/\s*<\/?string>\s*/).
                          reject { |v| v.match(/^\s*$/) }.
                          map { |v| v.strip.downcase }

            keywords[groups[m[0]]].concat values
          end

          keywords
        end

        def render_output(keywords, &b)
          return enum_for(:render_output, keywords).to_a.join("\n") unless b

          yield   "# -*- coding: utf-8 -*- #"
          yield   "# frozen_string_literal: true"
          yield   ""
          yield   "# DO NOT EDIT"
          yield   "# This file is automatically generated by `rake builtins:lasso`."
          yield   "# See tasks/builtins/lasso.rake for more info."
          yield   ""
          yield   "module Rouge"
          yield   "  module Lexers"
          yield   "    def Lasso.keywords"
          yield   "      @keywords ||= {}.tap do |h|"
          keywords.each do |key, value|
            yield "        h[#{key.inspect}] = Set.new #{value.inspect}"
          end
          yield   "      end"
          yield   "    end"
          yield   "  end"
          yield   "end"
        end
      end
    end
  end
end