File: mathematica.rake

package info (click to toggle)
ruby-rouge 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,836 kB
  • sloc: ruby: 38,168; sed: 2,071; perl: 152; makefile: 8
file content (60 lines) | stat: -rw-r--r-- 1,737 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
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

require 'open-uri'

MATHEMATICA_SYNTAX_URI = "https://reference.wolfram.com/language/guide/AlphabeticalListing.html"
MATHEMATICA_KEYWORDS_FILE = "./lib/rouge/lexers/mathematica/keywords.rb"

namespace :builtins do
  task :mathematica do
    generator = Rouge::Tasks::Builtins::Mathematica.new

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

    output = generator.render_output(keywords)

    File.write(MATHEMATICA_KEYWORDS_FILE, output)
  end
end

module Rouge
  module Tasks
    module Builtins
      class Mathematica
        def extract_keywords(input)
          keywords = Array.new

          input.scrub.scan %r(<span class="IFSans"><a href="/language/ref/(\w+)[.]html">)m do |m|
            keywords.push m[0]
          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   ""
          yield   "# This file is automatically generated by `rake builtins:mathematica`."
          yield   "# See tasks/builtins/mathematica.rake for more info."
          yield   ""
          yield   "module Rouge"
          yield   "  module Lexers"
          yield   "    class Mathematica"
          yield   "      def self.builtins"
          yield   "        @builtins ||= Set.new #{keywords.inspect}"
          yield   "      end"
          yield   "    end"
          yield   "  end"
          yield   "end"
        end
      end
    end
  end
end