llm-shell 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +61 -66
- data/lib/llm/shell/command.rb +40 -40
- data/lib/llm/shell/commands/clear_screen.rb +4 -18
- data/lib/llm/shell/commands/debug_mode.rb +12 -0
- data/lib/llm/shell/commands/dir_import.rb +4 -20
- data/lib/llm/shell/commands/disable_tool.rb +33 -0
- data/lib/llm/shell/commands/enable_tool.rb +33 -0
- data/lib/llm/shell/commands/file_import.rb +4 -20
- data/lib/llm/shell/commands/help.rb +23 -36
- data/lib/llm/shell/commands/show_chat.rb +4 -19
- data/lib/llm/shell/commands/show_version.rb +4 -20
- data/lib/llm/shell/commands/system_prompt.rb +4 -18
- data/lib/llm/shell/completion.rb +5 -5
- data/lib/llm/shell/config.rb +4 -5
- data/lib/llm/shell/formatter.rb +1 -2
- data/lib/llm/shell/internal/coderay/lib/coderay/duo.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/_map.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/comment_filter.rb +25 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/count.rb +39 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug.rb +49 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug_lint.rb +63 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/div.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/encoder.rb +190 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/filter.rb +58 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/css.rb +65 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/numbering.rb +108 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/output.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html.rb +333 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/json.rb +83 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lines_of_code.rb +45 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lint.rb +59 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/null.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/page.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/span.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/statistic.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/terminal.rb +195 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/text.rb +46 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/xml.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/yaml.rb +50 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/for_redcloth.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/file_type.rb +151 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin_host.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/word_list.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/_map.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/c.rb +189 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/clojure.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/cpp.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/css.rb +196 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/debug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/delphi.rb +144 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/diff.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/erb.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/go.rb +208 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/groovy.rb +268 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/haml.rb +168 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/html.rb +275 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java/builtin_types.rb +421 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java.rb +174 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java_script.rb +236 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/json.rb +98 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/lua.rb +280 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/php.rb +527 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/python.rb +287 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/raydebug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/patterns.rb +178 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/string_state.rb +79 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby.rb +477 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sass.rb +232 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/scanner.rb +337 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sql.rb +169 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/taskpaper.rb +36 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/text.rb +26 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/xml.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/yaml.rb +140 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners.rb +27 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/_map.rb +7 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/alpha.rb +153 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/style.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles.rb +15 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/token_kinds.rb +85 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens_proxy.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/version.rb +3 -0
- data/lib/llm/shell/internal/coderay/lib/coderay.rb +284 -0
- data/lib/llm/shell/internal/io-line/lib/io/line/multiple.rb +19 -0
- data/lib/{io → llm/shell/internal/io-line/lib/io}/line.rb +2 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/builder.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/conversable.rb +37 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/completion.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/respond.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot.rb +150 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/buffer.rb +162 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/client.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/error.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/file.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/function.rb +177 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/message.rb +178 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/mime.rb +140 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/multipart.rb +101 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/builder.rb +38 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/kernel.rb +53 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object.rb +89 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/provider.rb +352 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/files.rb +155 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format/completion_format.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/models.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/completion.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/enumerable.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/file.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/stream_parser.rb +66 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic.rb +138 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format/completion_format.rb +68 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format.rb +27 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/audio.rb +73 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/error_handler.rb +47 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/files.rb +146 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/images.rb +133 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/models.rb +60 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/completion.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/embedding.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/file.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/files.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/image.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/models.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/web_search.rb +22 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb +173 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/llamacpp.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format/completion_format.rb +77 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/models.rb +56 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/completion.rb +28 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/stream_parser.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama.rb +116 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/audio.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/error_handler.rb +46 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/files.rb +134 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/completion_format.rb +90 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/moderation_format.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/respond_format.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/images.rb +109 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/models.rb +55 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/moderations.rb +65 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/audio.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/completion.rb +40 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/enumerable.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/file.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/image.rb +16 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/moderations.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/responds.rb +48 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses/stream_parser.rb +76 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses.rb +99 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/vector_stores.rb +228 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai.rb +206 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai/images.rb +58 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/zai.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/response.rb +67 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/array.rb +26 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/boolean.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/integer.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/leaf.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/null.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/number.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/object.rb +41 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/string.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/version.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema.rb +81 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/server_tool.rb +32 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool/param.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/utils.rb +19 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/version.rb +5 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm.rb +121 -0
- data/lib/llm/shell/internal/optparse/lib/optionparser.rb +2 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/ac.rb +70 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/date.rb +18 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/kwargs.rb +27 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/shellwords.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/time.rb +11 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/uri.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/version.rb +80 -0
- data/lib/llm/shell/internal/optparse/lib/optparse.rb +2469 -0
- data/lib/llm/shell/internal/paint/lib/paint/constants.rb +104 -0
- data/lib/llm/shell/internal/paint/lib/paint/pa.rb +13 -0
- data/lib/llm/shell/internal/paint/lib/paint/rgb_colors.rb +14 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts.rb +100 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts_version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint/util.rb +16 -0
- data/lib/llm/shell/internal/paint/lib/paint/version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint.rb +261 -0
- data/lib/llm/shell/internal/reline/lib/reline/config.rb +378 -0
- data/lib/llm/shell/internal/reline/lib/reline/face.rb +199 -0
- data/lib/llm/shell/internal/reline/lib/reline/history.rb +76 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/ansi.rb +322 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/dumb.rb +120 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/windows.rb +530 -0
- data/lib/llm/shell/internal/reline/lib/reline/io.rb +55 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/base.rb +37 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/composite.rb +17 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/emacs.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_command.rb +518 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_insert.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor.rb +8 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_stroke.rb +119 -0
- data/lib/llm/shell/internal/reline/lib/reline/kill_ring.rb +125 -0
- data/lib/llm/shell/internal/reline/lib/reline/line_editor.rb +2356 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode/east_asian_width.rb +1292 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode.rb +421 -0
- data/lib/llm/shell/internal/reline/lib/reline/version.rb +3 -0
- data/lib/llm/shell/internal/reline/lib/reline.rb +527 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/generated_parser.rb +712 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb +268 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date.rb +35 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date_time.rb +42 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_time.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/parser.rb +21 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/scanner.rb +92 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/string_utils.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/version.rb +5 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb.rb +49 -0
- data/lib/llm/shell/options.rb +1 -1
- data/lib/llm/shell/renderer.rb +2 -3
- data/lib/llm/shell/repl.rb +21 -16
- data/lib/llm/shell/tool.rb +42 -0
- data/lib/llm/shell/tools/read_file.rb +15 -0
- data/lib/llm/shell/tools/system.rb +17 -0
- data/lib/llm/shell/tools/write_file.rb +16 -0
- data/lib/llm/shell/version.rb +1 -1
- data/lib/llm/shell.rb +83 -39
- data/libexec/llm-shell/shell +4 -6
- data/llm-shell.gemspec +0 -4
- metadata +233 -63
- data/lib/llm/function.rb +0 -17
- data/lib/llm/shell/command/extension.rb +0 -42
- data/lib/llm/shell/commands/utils.rb +0 -21
- data/lib/llm/shell/functions/read_file.rb +0 -22
- data/lib/llm/shell/functions/write_file.rb +0 -22
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# = XML Encoder
|
|
5
|
+
#
|
|
6
|
+
# Uses REXML. Very slow.
|
|
7
|
+
class XML < Encoder
|
|
8
|
+
|
|
9
|
+
register_for :xml
|
|
10
|
+
|
|
11
|
+
FILE_EXTENSION = 'xml'
|
|
12
|
+
|
|
13
|
+
autoload :REXML, 'rexml/document'
|
|
14
|
+
|
|
15
|
+
DEFAULT_OPTIONS = {
|
|
16
|
+
:tab_width => 8,
|
|
17
|
+
:pretty => -1,
|
|
18
|
+
:transitive => false,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected
|
|
22
|
+
def setup options
|
|
23
|
+
super
|
|
24
|
+
|
|
25
|
+
@doc = REXML::Document.new
|
|
26
|
+
@doc << REXML::XMLDecl.new
|
|
27
|
+
@tab_width = options[:tab_width]
|
|
28
|
+
@root = @node = @doc.add_element('coderay-tokens')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def finish options
|
|
32
|
+
@doc.write @out, options[:pretty], options[:transitive], true
|
|
33
|
+
|
|
34
|
+
super
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
public
|
|
38
|
+
def text_token text, kind
|
|
39
|
+
if kind == :space
|
|
40
|
+
token = @node
|
|
41
|
+
else
|
|
42
|
+
token = @node.add_element kind.to_s
|
|
43
|
+
end
|
|
44
|
+
text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
|
|
45
|
+
case
|
|
46
|
+
when space
|
|
47
|
+
token << REXML::Text.new(space, true)
|
|
48
|
+
when tab
|
|
49
|
+
token << REXML::Text.new(tab, true)
|
|
50
|
+
when nl
|
|
51
|
+
token << REXML::Text.new(nl, true)
|
|
52
|
+
else
|
|
53
|
+
token << REXML::Text.new($&)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def begin_group kind
|
|
59
|
+
@node = @node.add_element kind.to_s
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def end_group kind
|
|
63
|
+
if @node == @root
|
|
64
|
+
raise 'no token to close!'
|
|
65
|
+
end
|
|
66
|
+
@node = @node.parent
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
autoload :YAML, 'yaml'
|
|
2
|
+
|
|
3
|
+
module CodeRay
|
|
4
|
+
module Encoders
|
|
5
|
+
|
|
6
|
+
# = YAML Encoder
|
|
7
|
+
#
|
|
8
|
+
# Slow.
|
|
9
|
+
class YAML < Encoder
|
|
10
|
+
|
|
11
|
+
register_for :yaml
|
|
12
|
+
|
|
13
|
+
FILE_EXTENSION = 'yaml'
|
|
14
|
+
|
|
15
|
+
protected
|
|
16
|
+
def setup options
|
|
17
|
+
super
|
|
18
|
+
|
|
19
|
+
@data = []
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def finish options
|
|
23
|
+
output ::YAML.dump(@data)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
public
|
|
27
|
+
def text_token text, kind
|
|
28
|
+
@data << [text, kind]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def begin_group kind
|
|
32
|
+
@data << [:begin_group, kind]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def end_group kind
|
|
36
|
+
@data << [:end_group, kind]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def begin_line kind
|
|
40
|
+
@data << [:begin_line, kind]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def end_line kind
|
|
44
|
+
@data << [:end_line, kind]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
|
|
3
|
+
# This module holds the Encoder class and its subclasses.
|
|
4
|
+
# For example, the HTML encoder is named CodeRay::Encoders::HTML
|
|
5
|
+
# can be found in coderay/encoders/html.
|
|
6
|
+
#
|
|
7
|
+
# Encoders also provides methods and constants for the register
|
|
8
|
+
# mechanism and the [] method that returns the Encoder class
|
|
9
|
+
# belonging to the given format.
|
|
10
|
+
module Encoders
|
|
11
|
+
|
|
12
|
+
extend PluginHost
|
|
13
|
+
plugin_path File.dirname(__FILE__), 'encoders'
|
|
14
|
+
|
|
15
|
+
autoload :Encoder, CodeRay.coderay_path('encoders', 'encoder')
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
|
|
3
|
+
# A little hack to enable CodeRay highlighting in RedCloth.
|
|
4
|
+
#
|
|
5
|
+
# Usage:
|
|
6
|
+
# require 'coderay'
|
|
7
|
+
# require 'coderay/for_redcloth'
|
|
8
|
+
# RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
|
|
9
|
+
#
|
|
10
|
+
# Make sure you have RedCloth 4.0.3 activated, for example by calling
|
|
11
|
+
# require 'rubygems'
|
|
12
|
+
# before RedCloth is loaded and before calling CodeRay.for_redcloth.
|
|
13
|
+
module ForRedCloth
|
|
14
|
+
|
|
15
|
+
def self.install
|
|
16
|
+
gem 'RedCloth', '>= 4.0.3' if defined? gem
|
|
17
|
+
require 'redcloth'
|
|
18
|
+
unless RedCloth::VERSION.to_s >= '4.0.3'
|
|
19
|
+
if defined? gem
|
|
20
|
+
raise 'CodeRay.for_redcloth needs RedCloth version 4.0.3 or later. ' +
|
|
21
|
+
"You have #{RedCloth::VERSION}. Please gem install RedCloth."
|
|
22
|
+
else
|
|
23
|
+
$".delete 'redcloth.rb' # sorry, but it works
|
|
24
|
+
require 'rubygems'
|
|
25
|
+
return install # retry
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
unless RedCloth::VERSION.to_s >= '4.2.2'
|
|
29
|
+
warn 'CodeRay.for_redcloth works best with RedCloth version 4.2.2 or later.'
|
|
30
|
+
end
|
|
31
|
+
RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc
|
|
32
|
+
RedCloth::Formatters::HTML.module_eval do
|
|
33
|
+
def unescape(html) # :nodoc:
|
|
34
|
+
replacements = {
|
|
35
|
+
'&' => '&',
|
|
36
|
+
'"' => '"',
|
|
37
|
+
'>' => '>',
|
|
38
|
+
'<' => '<',
|
|
39
|
+
}
|
|
40
|
+
html.gsub(/&(?:amp|quot|[gl]t);/) { |entity| replacements[entity] }
|
|
41
|
+
end
|
|
42
|
+
undef code, bc_open, bc_close, escape_pre
|
|
43
|
+
def code(opts) # :nodoc:
|
|
44
|
+
opts[:block] = true
|
|
45
|
+
if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0'
|
|
46
|
+
# simulating pre-4.2 behavior
|
|
47
|
+
if opts[:text].sub!(/\A\[(\w+)\]/, '')
|
|
48
|
+
if CodeRay::Scanners[$1].lang == :text
|
|
49
|
+
opts[:text] = $& + opts[:text]
|
|
50
|
+
else
|
|
51
|
+
opts[:lang] = $1
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
if opts[:lang] && !filter_coderay
|
|
56
|
+
require 'coderay'
|
|
57
|
+
@in_bc ||= nil
|
|
58
|
+
format = @in_bc ? :div : :span
|
|
59
|
+
opts[:text] = unescape(opts[:text]) unless @in_bc
|
|
60
|
+
highlighted_code = CodeRay.encode opts[:text], opts[:lang], format
|
|
61
|
+
highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) }
|
|
62
|
+
highlighted_code
|
|
63
|
+
else
|
|
64
|
+
"<code#{pba(opts)}>#{opts[:text]}</code>"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
def bc_open(opts) # :nodoc:
|
|
68
|
+
opts[:block] = true
|
|
69
|
+
@in_bc = opts
|
|
70
|
+
opts[:lang] ? '' : "<pre#{pba(opts)}>"
|
|
71
|
+
end
|
|
72
|
+
def bc_close(opts) # :nodoc:
|
|
73
|
+
opts = @in_bc
|
|
74
|
+
@in_bc = nil
|
|
75
|
+
opts[:lang] ? '' : "</pre>\n"
|
|
76
|
+
end
|
|
77
|
+
def escape_pre(text) # :nodoc:
|
|
78
|
+
if @in_bc ||= nil
|
|
79
|
+
text
|
|
80
|
+
else
|
|
81
|
+
html_esc(text, :html_escape_preformatted)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
module TextileDoc # :nodoc:
|
|
88
|
+
attr_accessor :filter_coderay
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
CodeRay::ForRedCloth.install
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
|
|
3
|
+
# = FileType
|
|
4
|
+
#
|
|
5
|
+
# A simple filetype recognizer.
|
|
6
|
+
#
|
|
7
|
+
# == Usage
|
|
8
|
+
#
|
|
9
|
+
# # determine the type of the given
|
|
10
|
+
# lang = FileType[file_name]
|
|
11
|
+
#
|
|
12
|
+
# # return :text if the file type is unknown
|
|
13
|
+
# lang = FileType.fetch file_name, :text
|
|
14
|
+
#
|
|
15
|
+
# # try the shebang line, too
|
|
16
|
+
# lang = FileType.fetch file_name, :text, true
|
|
17
|
+
module FileType
|
|
18
|
+
|
|
19
|
+
UnknownFileType = Class.new Exception
|
|
20
|
+
|
|
21
|
+
class << self
|
|
22
|
+
|
|
23
|
+
# Try to determine the file type of the file.
|
|
24
|
+
#
|
|
25
|
+
# +filename+ is a relative or absolute path to a file.
|
|
26
|
+
#
|
|
27
|
+
# The file itself is only accessed when +read_shebang+ is set to true.
|
|
28
|
+
# That means you can get filetypes from files that don't exist.
|
|
29
|
+
def [] filename, read_shebang = false
|
|
30
|
+
name = File.basename filename
|
|
31
|
+
ext = File.extname(name).sub(/^\./, '') # from last dot, delete the leading dot
|
|
32
|
+
ext2 = filename.to_s[/\.(.*)/, 1] # from first dot
|
|
33
|
+
|
|
34
|
+
type =
|
|
35
|
+
TypeFromExt[ext] ||
|
|
36
|
+
TypeFromExt[ext.downcase] ||
|
|
37
|
+
(TypeFromExt[ext2] if ext2) ||
|
|
38
|
+
(TypeFromExt[ext2.downcase] if ext2) ||
|
|
39
|
+
TypeFromName[name] ||
|
|
40
|
+
TypeFromName[name.downcase]
|
|
41
|
+
type ||= type_from_shebang(filename) if read_shebang
|
|
42
|
+
|
|
43
|
+
type
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# This works like Hash#fetch.
|
|
47
|
+
#
|
|
48
|
+
# If the filetype cannot be found, the +default+ value
|
|
49
|
+
# is returned.
|
|
50
|
+
def fetch filename, default = nil, read_shebang = false
|
|
51
|
+
if default && block_given?
|
|
52
|
+
warn 'Block supersedes default value argument; use either.'
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
if type = self[filename, read_shebang]
|
|
56
|
+
type
|
|
57
|
+
else
|
|
58
|
+
return yield if block_given?
|
|
59
|
+
return default if default
|
|
60
|
+
raise UnknownFileType, 'Could not determine type of %p.' % filename
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
protected
|
|
65
|
+
|
|
66
|
+
def type_from_shebang filename
|
|
67
|
+
return unless File.exist? filename
|
|
68
|
+
File.open filename, 'r' do |f|
|
|
69
|
+
if first_line = f.gets
|
|
70
|
+
if type = first_line[TypeFromShebang]
|
|
71
|
+
type.to_sym
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
TypeFromExt = {
|
|
80
|
+
'c' => :c,
|
|
81
|
+
'cfc' => :xml,
|
|
82
|
+
'cfm' => :xml,
|
|
83
|
+
'clj' => :clojure,
|
|
84
|
+
'css' => :css,
|
|
85
|
+
'diff' => :diff,
|
|
86
|
+
'dpr' => :delphi,
|
|
87
|
+
'erb' => :erb,
|
|
88
|
+
'gemspec' => :ruby,
|
|
89
|
+
'go' => :go,
|
|
90
|
+
'groovy' => :groovy,
|
|
91
|
+
'gvy' => :groovy,
|
|
92
|
+
'h' => :c,
|
|
93
|
+
'haml' => :haml,
|
|
94
|
+
'htm' => :html,
|
|
95
|
+
'html' => :html,
|
|
96
|
+
'html.erb' => :erb,
|
|
97
|
+
'java' => :java,
|
|
98
|
+
'js' => :java_script,
|
|
99
|
+
'json' => :json,
|
|
100
|
+
'lua' => :lua,
|
|
101
|
+
'mab' => :ruby,
|
|
102
|
+
'pas' => :delphi,
|
|
103
|
+
'patch' => :diff,
|
|
104
|
+
'phtml' => :php,
|
|
105
|
+
'php' => :php,
|
|
106
|
+
'php3' => :php,
|
|
107
|
+
'php4' => :php,
|
|
108
|
+
'php5' => :php,
|
|
109
|
+
'prawn' => :ruby,
|
|
110
|
+
'py' => :python,
|
|
111
|
+
'py3' => :python,
|
|
112
|
+
'pyw' => :python,
|
|
113
|
+
'rake' => :ruby,
|
|
114
|
+
'raydebug' => :raydebug,
|
|
115
|
+
'rb' => :ruby,
|
|
116
|
+
'rbw' => :ruby,
|
|
117
|
+
'rhtml' => :erb,
|
|
118
|
+
'rjs' => :ruby,
|
|
119
|
+
'rpdf' => :ruby,
|
|
120
|
+
'ru' => :ruby, # config.ru
|
|
121
|
+
'rxml' => :ruby,
|
|
122
|
+
'sass' => :sass,
|
|
123
|
+
'sql' => :sql,
|
|
124
|
+
'taskpaper' => :taskpaper,
|
|
125
|
+
'template' => :json, # AWS CloudFormation template
|
|
126
|
+
'tmproj' => :xml,
|
|
127
|
+
'xaml' => :xml,
|
|
128
|
+
'xhtml' => :html,
|
|
129
|
+
'xml' => :xml,
|
|
130
|
+
'yaml' => :yaml,
|
|
131
|
+
'yml' => :yaml,
|
|
132
|
+
}
|
|
133
|
+
for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
|
|
134
|
+
TypeFromExt[cpp_alias] = :cpp
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
|
|
138
|
+
|
|
139
|
+
TypeFromName = {
|
|
140
|
+
'Capfile' => :ruby,
|
|
141
|
+
'Rakefile' => :ruby,
|
|
142
|
+
'Rantfile' => :ruby,
|
|
143
|
+
'Gemfile' => :ruby,
|
|
144
|
+
'Guardfile' => :ruby,
|
|
145
|
+
'Vagrantfile' => :ruby,
|
|
146
|
+
'Appraisals' => :ruby
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
|
|
3
|
+
# = Plugin
|
|
4
|
+
#
|
|
5
|
+
# Plugins have to include this module.
|
|
6
|
+
#
|
|
7
|
+
# IMPORTANT: Use extend for this module.
|
|
8
|
+
#
|
|
9
|
+
# See CodeRay::PluginHost for examples.
|
|
10
|
+
module Plugin
|
|
11
|
+
|
|
12
|
+
attr_reader :plugin_id
|
|
13
|
+
|
|
14
|
+
# Register this class for the given +id+.
|
|
15
|
+
#
|
|
16
|
+
# Example:
|
|
17
|
+
# class MyPlugin < PluginHost::BaseClass
|
|
18
|
+
# register_for :my_id
|
|
19
|
+
# ...
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# See PluginHost.register.
|
|
23
|
+
def register_for id
|
|
24
|
+
@plugin_id = id
|
|
25
|
+
plugin_host.register self, id
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Returns the title of the plugin, or sets it to the
|
|
29
|
+
# optional argument +title+.
|
|
30
|
+
def title title = nil
|
|
31
|
+
if title
|
|
32
|
+
@title = title.to_s
|
|
33
|
+
else
|
|
34
|
+
@title ||= name[/([^:]+)$/, 1]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The PluginHost for this Plugin class.
|
|
39
|
+
def plugin_host host = nil
|
|
40
|
+
if host.is_a? PluginHost
|
|
41
|
+
const_set :PLUGIN_HOST, host
|
|
42
|
+
end
|
|
43
|
+
self::PLUGIN_HOST
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def aliases
|
|
47
|
+
plugin_host.plugin_hash.inject [] do |aliases, (key, _)|
|
|
48
|
+
aliases << key if plugin_host[key] == self
|
|
49
|
+
aliases
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
|
|
3
|
+
# = PluginHost
|
|
4
|
+
#
|
|
5
|
+
# A simple subclass/subfolder plugin system.
|
|
6
|
+
#
|
|
7
|
+
# Example:
|
|
8
|
+
# class Generators
|
|
9
|
+
# extend PluginHost
|
|
10
|
+
# plugin_path 'app/generators'
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# class Generator
|
|
14
|
+
# extend Plugin
|
|
15
|
+
# PLUGIN_HOST = Generators
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# class FancyGenerator < Generator
|
|
19
|
+
# register_for :fancy
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# Generators[:fancy] #-> FancyGenerator
|
|
23
|
+
# # or
|
|
24
|
+
# CodeRay.require_plugin 'Generators/fancy'
|
|
25
|
+
# # or
|
|
26
|
+
# Generators::Fancy
|
|
27
|
+
module PluginHost
|
|
28
|
+
|
|
29
|
+
# Raised if Encoders::[] fails because:
|
|
30
|
+
# * a file could not be found
|
|
31
|
+
# * the requested Plugin is not registered
|
|
32
|
+
PluginNotFound = Class.new LoadError
|
|
33
|
+
HostNotFound = Class.new LoadError
|
|
34
|
+
|
|
35
|
+
PLUGIN_HOSTS = []
|
|
36
|
+
PLUGIN_HOSTS_BY_ID = {} # dummy hash
|
|
37
|
+
|
|
38
|
+
# Loads all plugins using list and load.
|
|
39
|
+
def load_all
|
|
40
|
+
for plugin in list
|
|
41
|
+
load plugin
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Returns the Plugin for +id+.
|
|
46
|
+
#
|
|
47
|
+
# Example:
|
|
48
|
+
# yaml_plugin = MyPluginHost[:yaml]
|
|
49
|
+
def [] id, *args, &blk
|
|
50
|
+
plugin = validate_id(id)
|
|
51
|
+
begin
|
|
52
|
+
plugin = plugin_hash.[](plugin, *args, &blk)
|
|
53
|
+
end while plugin.is_a? String
|
|
54
|
+
plugin
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
alias load []
|
|
58
|
+
|
|
59
|
+
# Tries to +load+ the missing plugin by translating +const+ to the
|
|
60
|
+
# underscore form (eg. LinesOfCode becomes lines_of_code).
|
|
61
|
+
def const_missing const
|
|
62
|
+
id = const.to_s.
|
|
63
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
|
64
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
|
65
|
+
downcase
|
|
66
|
+
load id
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
class << self
|
|
70
|
+
|
|
71
|
+
# Adds the module/class to the PLUGIN_HOSTS list.
|
|
72
|
+
def extended mod
|
|
73
|
+
PLUGIN_HOSTS << mod
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The path where the plugins can be found.
|
|
79
|
+
def plugin_path *args
|
|
80
|
+
unless args.empty?
|
|
81
|
+
@plugin_path = File.expand_path File.join(*args)
|
|
82
|
+
end
|
|
83
|
+
@plugin_path ||= ''
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Map a plugin_id to another.
|
|
87
|
+
#
|
|
88
|
+
# Usage: Put this in a file plugin_path/_map.rb.
|
|
89
|
+
#
|
|
90
|
+
# class MyColorHost < PluginHost
|
|
91
|
+
# map :navy => :dark_blue,
|
|
92
|
+
# :maroon => :brown,
|
|
93
|
+
# :luna => :moon
|
|
94
|
+
# end
|
|
95
|
+
def map hash
|
|
96
|
+
for from, to in hash
|
|
97
|
+
from = validate_id from
|
|
98
|
+
to = validate_id to
|
|
99
|
+
plugin_hash[from] = to unless plugin_hash.has_key? from
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Define the default plugin to use when no plugin is found
|
|
104
|
+
# for a given id, or return the default plugin.
|
|
105
|
+
#
|
|
106
|
+
# See also map.
|
|
107
|
+
#
|
|
108
|
+
# class MyColorHost < PluginHost
|
|
109
|
+
# map :navy => :dark_blue
|
|
110
|
+
# default :gray
|
|
111
|
+
# end
|
|
112
|
+
#
|
|
113
|
+
# MyColorHost.default # loads and returns the Gray plugin
|
|
114
|
+
def default id = nil
|
|
115
|
+
if id
|
|
116
|
+
id = validate_id id
|
|
117
|
+
raise "The default plugin can't be named \"default\"." if id == :default
|
|
118
|
+
plugin_hash[:default] = id
|
|
119
|
+
else
|
|
120
|
+
load :default
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Every plugin must register itself for +id+ by calling register_for,
|
|
125
|
+
# which calls this method.
|
|
126
|
+
#
|
|
127
|
+
# See Plugin#register_for.
|
|
128
|
+
def register plugin, id
|
|
129
|
+
plugin_hash[validate_id(id)] = plugin
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# A Hash of plugion_id => Plugin pairs.
|
|
133
|
+
def plugin_hash
|
|
134
|
+
@plugin_hash ||= (@plugin_hash = make_plugin_hash).tap { load_plugin_map }
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Returns an array of all .rb files in the plugin path.
|
|
138
|
+
#
|
|
139
|
+
# The extension .rb is not included.
|
|
140
|
+
def list
|
|
141
|
+
Dir[path_to('*')].select do |file|
|
|
142
|
+
File.basename(file)[/^(?!_)\w+\.rb$/]
|
|
143
|
+
end.map do |file|
|
|
144
|
+
File.basename(file, '.rb').to_sym
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Returns an array of all Plugins.
|
|
149
|
+
#
|
|
150
|
+
# Note: This loads all plugins using load_all.
|
|
151
|
+
def all_plugins
|
|
152
|
+
load_all
|
|
153
|
+
plugin_hash.values.grep(Class)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Loads the map file (see map).
|
|
157
|
+
#
|
|
158
|
+
# This is done automatically when plugin_path is called.
|
|
159
|
+
def load_plugin_map
|
|
160
|
+
mapfile = path_to '_map'
|
|
161
|
+
if File.exist? mapfile
|
|
162
|
+
require mapfile
|
|
163
|
+
true
|
|
164
|
+
else
|
|
165
|
+
false
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
protected
|
|
170
|
+
|
|
171
|
+
# Return a plugin hash that automatically loads plugins.
|
|
172
|
+
def make_plugin_hash
|
|
173
|
+
Hash.new do |h, plugin_id|
|
|
174
|
+
id = validate_id(plugin_id)
|
|
175
|
+
path = path_to id
|
|
176
|
+
begin
|
|
177
|
+
require path
|
|
178
|
+
rescue LoadError => boom
|
|
179
|
+
if h.has_key?(:default)
|
|
180
|
+
h[:default]
|
|
181
|
+
else
|
|
182
|
+
raise PluginNotFound, '%p could not load plugin %p: %s' % [self, id, boom]
|
|
183
|
+
end
|
|
184
|
+
else
|
|
185
|
+
# Plugin should have registered by now
|
|
186
|
+
if h.has_key? id
|
|
187
|
+
h[id]
|
|
188
|
+
else
|
|
189
|
+
raise PluginNotFound, "No #{self.name} plugin for #{id.inspect} found in #{path}."
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# Returns the expected path to the plugin file for the given id.
|
|
196
|
+
def path_to plugin_id
|
|
197
|
+
File.join plugin_path, "#{plugin_id}.rb"
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Converts +id+ to a valid plugin ID String, or returns +nil+.
|
|
201
|
+
#
|
|
202
|
+
# Raises +ArgumentError+ for all other objects, or if the
|
|
203
|
+
# given String includes non-alphanumeric characters (\W).
|
|
204
|
+
def validate_id id
|
|
205
|
+
case id
|
|
206
|
+
when Symbol
|
|
207
|
+
id.to_s
|
|
208
|
+
when String
|
|
209
|
+
if id[/\w+/] == id
|
|
210
|
+
id.downcase
|
|
211
|
+
else
|
|
212
|
+
raise ArgumentError, "Invalid id given: #{id}"
|
|
213
|
+
end
|
|
214
|
+
else
|
|
215
|
+
raise ArgumentError, "Symbol or String expected, but #{id.class} given."
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
end
|