|
| 1 | +require 'cgi' |
| 2 | +require 'kramdown' |
| 3 | +require 'kramdown-parser-gfm' |
| 4 | + |
| 5 | +module GraalPy |
| 6 | + class PythonOptions < Liquid::Tag |
| 7 | + def initialize(tag_name, text, tokens) |
| 8 | + super |
| 9 | + @text = text.strip |
| 10 | + end |
| 11 | + |
| 12 | + def render(context) |
| 13 | + content = File.read(@text) |
| 14 | + options = {} |
| 15 | + |
| 16 | + regexp = / |
| 17 | + ^(\s*@EngineOption\s*)? # Optional @EngineOption |
| 18 | + \s*@Option\s*\( # @Option( |
| 19 | + (.*?) # non-greedy match content |
| 20 | + \)\s*(?:\/\/)?\s* # ) |
| 21 | + public\s+static\s+final\s+OptionKey<[^>]+>\s+(\w+) # field name (e.g. InitialLocale) |
| 22 | + /mx |
| 23 | + |
| 24 | + help_regexp = / |
| 25 | + help\s*=\s*((?:"[^"]*"\s*(?:\+\s*"[^"]*"\s*)*)) # help = "..."+ "...", |
| 26 | + /mx |
| 27 | + |
| 28 | + stability_regexp = /stability\s*=\s*OptionStability\.(\w+)/ |
| 29 | + |
| 30 | + category_regexp = /category\s*=\s*OptionCategory\.(\w+)/ |
| 31 | + |
| 32 | + usage_regexp = /usageSyntax\s*=\s*((?:"[^"]*"\s*(?:\+\s*"[^"]*"\s*)*))/ |
| 33 | + |
| 34 | + content.scan(regexp) do |engine_option, content, field| |
| 35 | + engine_option = !!engine_option |
| 36 | + |
| 37 | + help = content.match(help_regexp) |
| 38 | + help = help[1].scan(/"([^"]*)"/).flatten.join(" ").gsub(/\s+/, " ").strip if help |
| 39 | + |
| 40 | + stable = content.match(stability_regexp) |
| 41 | + stable = stable[1] == "STABLE" if stable |
| 42 | + |
| 43 | + category = content.match(category_regexp) |
| 44 | + category = category[1] if category |
| 45 | + |
| 46 | + usage = content.match(usage_regexp) |
| 47 | + usage = usage[1].scan(/"([^"]*)"/).flatten.join(" ").gsub(/\s+/, " ").strip if usage |
| 48 | + |
| 49 | + if help && !help.empty? |
| 50 | + help << "." unless help.end_with? "." |
| 51 | + help = CGI::escapeHTML help |
| 52 | + help << " Accepts: <code>#{CGI::escapeHTML usage}</code>" if usage and !usage.empty? |
| 53 | + options[field] = { help: help, category: category, stable: stable, engine_option: engine_option } |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + doc_content = options.select do |name, opt| |
| 58 | + opt[:stable] && ["USER", "EXPERT"].include?(opt[:category]) |
| 59 | + end.sort_by do |name, opt| |
| 60 | + [opt[:category] == "USER" ? 0 : 1, name.downcase] |
| 61 | + end.map do |name, opt| |
| 62 | + <<~HTML |
| 63 | + <p> |
| 64 | + <strong>#{name}</strong> |
| 65 | + #{' (Has to be the same for all Contexts in an Engine)' if opt[:engine_option]} |
| 66 | + </p> |
| 67 | + <p> |
| 68 | + #{opt[:help]} |
| 69 | + </p> |
| 70 | + HTML |
| 71 | + end.join("\n") |
| 72 | + end |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +Liquid::Template.register_tag('python_options', GraalPy::PythonOptions) |
0 commit comments