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,83 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# A simple JSON Encoder.
|
|
5
|
+
#
|
|
6
|
+
# Example:
|
|
7
|
+
# CodeRay.scan('puts "Hello world!"', :ruby).json
|
|
8
|
+
# yields
|
|
9
|
+
# [
|
|
10
|
+
# {"type"=>"text", "text"=>"puts", "kind"=>"ident"},
|
|
11
|
+
# {"type"=>"text", "text"=>" ", "kind"=>"space"},
|
|
12
|
+
# {"type"=>"block", "action"=>"open", "kind"=>"string"},
|
|
13
|
+
# {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
|
|
14
|
+
# {"type"=>"text", "text"=>"Hello world!", "kind"=>"content"},
|
|
15
|
+
# {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
|
|
16
|
+
# {"type"=>"block", "action"=>"close", "kind"=>"string"},
|
|
17
|
+
# ]
|
|
18
|
+
class JSON < Encoder
|
|
19
|
+
|
|
20
|
+
begin
|
|
21
|
+
require 'json'
|
|
22
|
+
rescue LoadError
|
|
23
|
+
begin
|
|
24
|
+
require 'rubygems' unless defined? Gem
|
|
25
|
+
gem 'json'
|
|
26
|
+
require 'json'
|
|
27
|
+
rescue LoadError
|
|
28
|
+
$stderr.puts "The JSON encoder needs the JSON library.\n" \
|
|
29
|
+
"Please gem install json."
|
|
30
|
+
raise
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
register_for :json
|
|
35
|
+
FILE_EXTENSION = 'json'
|
|
36
|
+
|
|
37
|
+
protected
|
|
38
|
+
def setup options
|
|
39
|
+
super
|
|
40
|
+
|
|
41
|
+
@first = true
|
|
42
|
+
@out << '['
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def finish options
|
|
46
|
+
@out << ']'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def append data
|
|
50
|
+
if @first
|
|
51
|
+
@first = false
|
|
52
|
+
else
|
|
53
|
+
@out << ','
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
@out << data.to_json
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
public
|
|
60
|
+
def text_token text, kind
|
|
61
|
+
append :type => 'text', :text => text, :kind => kind
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def begin_group kind
|
|
65
|
+
append :type => 'block', :action => 'open', :kind => kind
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def end_group kind
|
|
69
|
+
append :type => 'block', :action => 'close', :kind => kind
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def begin_line kind
|
|
73
|
+
append :type => 'block', :action => 'begin_line', :kind => kind
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def end_line kind
|
|
77
|
+
append :type => 'block', :action => 'end_line', :kind => kind
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# Counts the LoC (Lines of Code). Returns an Integer >= 0.
|
|
5
|
+
#
|
|
6
|
+
# Alias: +loc+
|
|
7
|
+
#
|
|
8
|
+
# Everything that is not comment, markup, doctype/shebang, or an empty line,
|
|
9
|
+
# is considered to be code.
|
|
10
|
+
#
|
|
11
|
+
# For example,
|
|
12
|
+
# * HTML files not containing JavaScript have 0 LoC
|
|
13
|
+
# * in a Java class without comments, LoC is the number of non-empty lines
|
|
14
|
+
#
|
|
15
|
+
# A Scanner class should define the token kinds that are not code in the
|
|
16
|
+
# KINDS_NOT_LOC constant, which defaults to [:comment, :doctype].
|
|
17
|
+
class LinesOfCode < TokenKindFilter
|
|
18
|
+
|
|
19
|
+
register_for :lines_of_code
|
|
20
|
+
|
|
21
|
+
NON_EMPTY_LINE = /^\s*\S.*$/
|
|
22
|
+
|
|
23
|
+
protected
|
|
24
|
+
|
|
25
|
+
def setup options
|
|
26
|
+
if scanner
|
|
27
|
+
kinds_not_loc = scanner.class::KINDS_NOT_LOC
|
|
28
|
+
else
|
|
29
|
+
warn "Tokens have no associated scanner, counting all nonempty lines." if $VERBOSE
|
|
30
|
+
kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
options[:exclude] = kinds_not_loc
|
|
34
|
+
|
|
35
|
+
super options
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def finish options
|
|
39
|
+
output @tokens.text.scan(NON_EMPTY_LINE).size
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# = Lint Encoder
|
|
5
|
+
#
|
|
6
|
+
# Checks for:
|
|
7
|
+
#
|
|
8
|
+
# - empty tokens
|
|
9
|
+
# - incorrect nesting
|
|
10
|
+
#
|
|
11
|
+
# It will raise an InvalidTokenStream exception when any of the above occurs.
|
|
12
|
+
#
|
|
13
|
+
# See also: Encoders::DebugLint
|
|
14
|
+
class Lint < Debug
|
|
15
|
+
|
|
16
|
+
register_for :lint
|
|
17
|
+
|
|
18
|
+
InvalidTokenStream = Class.new StandardError
|
|
19
|
+
EmptyToken = Class.new InvalidTokenStream
|
|
20
|
+
UnknownTokenKind = Class.new InvalidTokenStream
|
|
21
|
+
IncorrectTokenGroupNesting = Class.new InvalidTokenStream
|
|
22
|
+
|
|
23
|
+
def text_token text, kind
|
|
24
|
+
raise EmptyToken, 'empty token for %p' % [kind] if text.empty?
|
|
25
|
+
raise UnknownTokenKind, 'unknown token kind %p (text was %p)' % [kind, text] unless TokenKinds.has_key? kind
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def begin_group kind
|
|
29
|
+
@opened << kind
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def end_group kind
|
|
33
|
+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
|
|
34
|
+
@opened.pop
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def begin_line kind
|
|
38
|
+
@opened << kind
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def end_line kind
|
|
42
|
+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
|
|
43
|
+
@opened.pop
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
protected
|
|
47
|
+
|
|
48
|
+
def setup options
|
|
49
|
+
@opened = []
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def finish options
|
|
53
|
+
raise 'Some tokens still open at end of token stream: %p' % [@opened] unless @opened.empty?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
load :html
|
|
5
|
+
|
|
6
|
+
# Wraps the output into a HTML page, using CSS classes and
|
|
7
|
+
# line numbers in the table format by default.
|
|
8
|
+
#
|
|
9
|
+
# See Encoders::HTML for available options.
|
|
10
|
+
class Page < HTML
|
|
11
|
+
|
|
12
|
+
FILE_EXTENSION = 'html'
|
|
13
|
+
|
|
14
|
+
register_for :page
|
|
15
|
+
|
|
16
|
+
DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
|
|
17
|
+
:css => :class,
|
|
18
|
+
:wrap => :page,
|
|
19
|
+
:line_numbers => :table
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
load :html
|
|
5
|
+
|
|
6
|
+
# Wraps HTML output into a SPAN element, using inline styles by default.
|
|
7
|
+
#
|
|
8
|
+
# See Encoders::HTML for available options.
|
|
9
|
+
class Span < HTML
|
|
10
|
+
|
|
11
|
+
FILE_EXTENSION = 'span.html'
|
|
12
|
+
|
|
13
|
+
register_for :span
|
|
14
|
+
|
|
15
|
+
DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
|
|
16
|
+
:css => :style,
|
|
17
|
+
:wrap => :span,
|
|
18
|
+
:line_numbers => false
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# Makes a statistic for the given tokens.
|
|
5
|
+
#
|
|
6
|
+
# Alias: +stats+
|
|
7
|
+
class Statistic < Encoder
|
|
8
|
+
|
|
9
|
+
register_for :statistic
|
|
10
|
+
|
|
11
|
+
attr_reader :type_stats, :real_token_count # :nodoc:
|
|
12
|
+
|
|
13
|
+
TypeStats = Struct.new :count, :size # :nodoc:
|
|
14
|
+
|
|
15
|
+
protected
|
|
16
|
+
|
|
17
|
+
def setup options
|
|
18
|
+
super
|
|
19
|
+
|
|
20
|
+
@type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
|
|
21
|
+
@real_token_count = 0
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
STATS = <<-STATS # :nodoc:
|
|
25
|
+
|
|
26
|
+
Code Statistics
|
|
27
|
+
|
|
28
|
+
Tokens %8d
|
|
29
|
+
Non-Whitespace %8d
|
|
30
|
+
Bytes Total %8d
|
|
31
|
+
|
|
32
|
+
Token Types (%d):
|
|
33
|
+
type count ratio size (average)
|
|
34
|
+
-------------------------------------------------------------
|
|
35
|
+
%s
|
|
36
|
+
STATS
|
|
37
|
+
|
|
38
|
+
TOKEN_TYPES_ROW = <<-TKR # :nodoc:
|
|
39
|
+
%-20s %8d %6.2f %% %5.1f
|
|
40
|
+
TKR
|
|
41
|
+
|
|
42
|
+
def finish options
|
|
43
|
+
all = @type_stats['TOTAL']
|
|
44
|
+
all_count, all_size = all.count, all.size
|
|
45
|
+
@type_stats.each do |type, stat|
|
|
46
|
+
stat.size /= stat.count.to_f
|
|
47
|
+
end
|
|
48
|
+
types_stats = @type_stats.sort_by { |k, v| [-v.count, k.to_s] }.map do |k, v|
|
|
49
|
+
TOKEN_TYPES_ROW % [k, v.count, 100.0 * v.count / all_count, v.size]
|
|
50
|
+
end.join
|
|
51
|
+
@out << STATS % [
|
|
52
|
+
all_count, @real_token_count, all_size,
|
|
53
|
+
@type_stats.delete_if { |k, v| k.is_a? String }.size,
|
|
54
|
+
types_stats
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
super
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
public
|
|
61
|
+
|
|
62
|
+
def text_token text, kind
|
|
63
|
+
@real_token_count += 1 unless kind == :space
|
|
64
|
+
@type_stats[kind].count += 1
|
|
65
|
+
@type_stats[kind].size += text.size
|
|
66
|
+
@type_stats['TOTAL'].size += text.size
|
|
67
|
+
@type_stats['TOTAL'].count += 1
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def begin_group kind
|
|
71
|
+
block_token ':begin_group', kind
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def end_group kind
|
|
75
|
+
block_token ':end_group', kind
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def begin_line kind
|
|
79
|
+
block_token ':begin_line', kind
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def end_line kind
|
|
83
|
+
block_token ':end_line', kind
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def block_token action, kind
|
|
87
|
+
@type_stats['TOTAL'].count += 1
|
|
88
|
+
@type_stats[action].count += 1
|
|
89
|
+
@type_stats[kind].count += 1
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# Outputs code highlighted for a color terminal.
|
|
5
|
+
#
|
|
6
|
+
# Note: This encoder is in beta. It currently doesn't use the Styles.
|
|
7
|
+
#
|
|
8
|
+
# Alias: +term+
|
|
9
|
+
#
|
|
10
|
+
# == Authors & License
|
|
11
|
+
#
|
|
12
|
+
# By Rob Aldred (http://robaldred.co.uk)
|
|
13
|
+
#
|
|
14
|
+
# Based on idea by Nathan Weizenbaum (http://nex-3.com)
|
|
15
|
+
#
|
|
16
|
+
# MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
17
|
+
class Terminal < Encoder
|
|
18
|
+
|
|
19
|
+
register_for :terminal
|
|
20
|
+
|
|
21
|
+
TOKEN_COLORS = {
|
|
22
|
+
:debug => "\e[1;37;44m",
|
|
23
|
+
|
|
24
|
+
:annotation => "\e[34m",
|
|
25
|
+
:attribute_name => "\e[35m",
|
|
26
|
+
:attribute_value => "\e[31m",
|
|
27
|
+
:binary => {
|
|
28
|
+
:self => "\e[31m",
|
|
29
|
+
:char => "\e[1;31m",
|
|
30
|
+
:delimiter => "\e[1;31m",
|
|
31
|
+
},
|
|
32
|
+
:char => {
|
|
33
|
+
:self => "\e[35m",
|
|
34
|
+
:delimiter => "\e[1;35m"
|
|
35
|
+
},
|
|
36
|
+
:class => "\e[1;35;4m",
|
|
37
|
+
:class_variable => "\e[36m",
|
|
38
|
+
:color => "\e[32m",
|
|
39
|
+
:comment => {
|
|
40
|
+
:self => "\e[1;30m",
|
|
41
|
+
:char => "\e[37m",
|
|
42
|
+
:delimiter => "\e[37m",
|
|
43
|
+
},
|
|
44
|
+
:constant => "\e[1;34;4m",
|
|
45
|
+
:decorator => "\e[35m",
|
|
46
|
+
:definition => "\e[1;33m",
|
|
47
|
+
:directive => "\e[33m",
|
|
48
|
+
:docstring => "\e[31m",
|
|
49
|
+
:doctype => "\e[1;34m",
|
|
50
|
+
:done => "\e[1;30;2m",
|
|
51
|
+
:entity => "\e[31m",
|
|
52
|
+
:error => "\e[1;37;41m",
|
|
53
|
+
:exception => "\e[1;31m",
|
|
54
|
+
:float => "\e[1;35m",
|
|
55
|
+
:function => "\e[1;34m",
|
|
56
|
+
:global_variable => "\e[1;32m",
|
|
57
|
+
:hex => "\e[1;36m",
|
|
58
|
+
:id => "\e[1;34m",
|
|
59
|
+
:include => "\e[31m",
|
|
60
|
+
:integer => "\e[1;34m",
|
|
61
|
+
:imaginary => "\e[1;34m",
|
|
62
|
+
:important => "\e[1;31m",
|
|
63
|
+
:key => {
|
|
64
|
+
:self => "\e[35m",
|
|
65
|
+
:char => "\e[1;35m",
|
|
66
|
+
:delimiter => "\e[1;35m",
|
|
67
|
+
},
|
|
68
|
+
:keyword => "\e[32m",
|
|
69
|
+
:label => "\e[1;33m",
|
|
70
|
+
:local_variable => "\e[33m",
|
|
71
|
+
:namespace => "\e[1;35m",
|
|
72
|
+
:octal => "\e[1;34m",
|
|
73
|
+
:predefined => "\e[36m",
|
|
74
|
+
:predefined_constant => "\e[1;36m",
|
|
75
|
+
:predefined_type => "\e[1;32m",
|
|
76
|
+
:preprocessor => "\e[1;36m",
|
|
77
|
+
:pseudo_class => "\e[1;34m",
|
|
78
|
+
:regexp => {
|
|
79
|
+
:self => "\e[35m",
|
|
80
|
+
:delimiter => "\e[1;35m",
|
|
81
|
+
:modifier => "\e[35m",
|
|
82
|
+
:char => "\e[1;35m",
|
|
83
|
+
},
|
|
84
|
+
:reserved => "\e[32m",
|
|
85
|
+
:shell => {
|
|
86
|
+
:self => "\e[33m",
|
|
87
|
+
:char => "\e[1;33m",
|
|
88
|
+
:delimiter => "\e[1;33m",
|
|
89
|
+
:escape => "\e[1;33m",
|
|
90
|
+
},
|
|
91
|
+
:string => {
|
|
92
|
+
:self => "\e[31m",
|
|
93
|
+
:modifier => "\e[1;31m",
|
|
94
|
+
:char => "\e[1;35m",
|
|
95
|
+
:delimiter => "\e[1;31m",
|
|
96
|
+
:escape => "\e[1;31m",
|
|
97
|
+
},
|
|
98
|
+
:symbol => {
|
|
99
|
+
:self => "\e[33m",
|
|
100
|
+
:delimiter => "\e[1;33m",
|
|
101
|
+
},
|
|
102
|
+
:tag => "\e[32m",
|
|
103
|
+
:type => "\e[1;34m",
|
|
104
|
+
:value => "\e[36m",
|
|
105
|
+
:variable => "\e[34m",
|
|
106
|
+
|
|
107
|
+
:insert => {
|
|
108
|
+
:self => "\e[42m",
|
|
109
|
+
:insert => "\e[1;32;42m",
|
|
110
|
+
:eyecatcher => "\e[102m",
|
|
111
|
+
},
|
|
112
|
+
:delete => {
|
|
113
|
+
:self => "\e[41m",
|
|
114
|
+
:delete => "\e[1;31;41m",
|
|
115
|
+
:eyecatcher => "\e[101m",
|
|
116
|
+
},
|
|
117
|
+
:change => {
|
|
118
|
+
:self => "\e[44m",
|
|
119
|
+
:change => "\e[37;44m",
|
|
120
|
+
},
|
|
121
|
+
:head => {
|
|
122
|
+
:self => "\e[45m",
|
|
123
|
+
:filename => "\e[37;45m"
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
|
|
128
|
+
TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
|
|
129
|
+
TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
|
|
130
|
+
|
|
131
|
+
protected
|
|
132
|
+
|
|
133
|
+
def setup(options)
|
|
134
|
+
super
|
|
135
|
+
@opened = []
|
|
136
|
+
@color_scopes = [TOKEN_COLORS]
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
public
|
|
140
|
+
|
|
141
|
+
def text_token text, kind
|
|
142
|
+
if color = @color_scopes.last[kind]
|
|
143
|
+
color = color[:self] if color.is_a? Hash
|
|
144
|
+
|
|
145
|
+
@out << color
|
|
146
|
+
@out << (text.index("\n") ? text.gsub("\n", "\e[0m\n" + color) : text)
|
|
147
|
+
@out << "\e[0m"
|
|
148
|
+
if outer_color = @color_scopes.last[:self]
|
|
149
|
+
@out << outer_color
|
|
150
|
+
end
|
|
151
|
+
else
|
|
152
|
+
@out << text
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def begin_group kind
|
|
157
|
+
@opened << kind
|
|
158
|
+
@out << open_token(kind)
|
|
159
|
+
end
|
|
160
|
+
alias begin_line begin_group
|
|
161
|
+
|
|
162
|
+
def end_group kind
|
|
163
|
+
if @opened.pop
|
|
164
|
+
@color_scopes.pop
|
|
165
|
+
@out << "\e[0m"
|
|
166
|
+
if outer_color = @color_scopes.last[:self]
|
|
167
|
+
@out << outer_color
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def end_line kind
|
|
173
|
+
@out << (@line_filler ||= "\t" * 100)
|
|
174
|
+
end_group kind
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
private
|
|
178
|
+
|
|
179
|
+
def open_token kind
|
|
180
|
+
if color = @color_scopes.last[kind]
|
|
181
|
+
if color.is_a? Hash
|
|
182
|
+
@color_scopes << color
|
|
183
|
+
color[:self]
|
|
184
|
+
else
|
|
185
|
+
@color_scopes << @color_scopes.last
|
|
186
|
+
color
|
|
187
|
+
end
|
|
188
|
+
else
|
|
189
|
+
@color_scopes << @color_scopes.last
|
|
190
|
+
''
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# Concats the tokens into a single string, resulting in the original
|
|
5
|
+
# code string if no tokens were removed.
|
|
6
|
+
#
|
|
7
|
+
# Alias: +plain+, +plaintext+
|
|
8
|
+
#
|
|
9
|
+
# == Options
|
|
10
|
+
#
|
|
11
|
+
# === :separator
|
|
12
|
+
# A separator string to join the tokens.
|
|
13
|
+
#
|
|
14
|
+
# Default: empty String
|
|
15
|
+
class Text < Encoder
|
|
16
|
+
|
|
17
|
+
register_for :text
|
|
18
|
+
|
|
19
|
+
FILE_EXTENSION = 'txt'
|
|
20
|
+
|
|
21
|
+
DEFAULT_OPTIONS = {
|
|
22
|
+
:separator => nil
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
def text_token text, kind
|
|
26
|
+
super
|
|
27
|
+
|
|
28
|
+
if @first
|
|
29
|
+
@first = false
|
|
30
|
+
else
|
|
31
|
+
@out << @sep
|
|
32
|
+
end if @sep
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
protected
|
|
36
|
+
def setup options
|
|
37
|
+
super
|
|
38
|
+
|
|
39
|
+
@first = true
|
|
40
|
+
@sep = options[:separator]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
load :filter
|
|
5
|
+
|
|
6
|
+
# A Filter that selects tokens based on their token kind.
|
|
7
|
+
#
|
|
8
|
+
# == Options
|
|
9
|
+
#
|
|
10
|
+
# === :exclude
|
|
11
|
+
#
|
|
12
|
+
# One or many symbols (in an Array) which shall be excluded.
|
|
13
|
+
#
|
|
14
|
+
# Default: []
|
|
15
|
+
#
|
|
16
|
+
# === :include
|
|
17
|
+
#
|
|
18
|
+
# One or many symbols (in an array) which shall be included.
|
|
19
|
+
#
|
|
20
|
+
# Default: :all, which means all tokens are included.
|
|
21
|
+
#
|
|
22
|
+
# Exclusion wins over inclusion.
|
|
23
|
+
#
|
|
24
|
+
# See also: CommentFilter
|
|
25
|
+
class TokenKindFilter < Filter
|
|
26
|
+
|
|
27
|
+
register_for :token_kind_filter
|
|
28
|
+
|
|
29
|
+
DEFAULT_OPTIONS = {
|
|
30
|
+
:exclude => [],
|
|
31
|
+
:include => :all
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected
|
|
35
|
+
def setup options
|
|
36
|
+
super
|
|
37
|
+
|
|
38
|
+
@group_excluded = false
|
|
39
|
+
@exclude = options[:exclude]
|
|
40
|
+
@exclude = Array(@exclude) unless @exclude == :all
|
|
41
|
+
@include = options[:include]
|
|
42
|
+
@include = Array(@include) unless @include == :all
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def include_text_token? text, kind
|
|
46
|
+
include_group? kind
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def include_group? kind
|
|
50
|
+
(@include == :all || @include.include?(kind)) &&
|
|
51
|
+
!(@exclude == :all || @exclude.include?(kind))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
public
|
|
55
|
+
|
|
56
|
+
# Add the token to the output stream if +kind+ matches the conditions.
|
|
57
|
+
def text_token text, kind
|
|
58
|
+
super if !@group_excluded && include_text_token?(text, kind)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Add the token group to the output stream if +kind+ matches the
|
|
62
|
+
# conditions.
|
|
63
|
+
#
|
|
64
|
+
# If it does not, all tokens inside the group are excluded from the
|
|
65
|
+
# stream, even if their kinds match.
|
|
66
|
+
def begin_group kind
|
|
67
|
+
if @group_excluded
|
|
68
|
+
@group_excluded += 1
|
|
69
|
+
elsif include_group? kind
|
|
70
|
+
super
|
|
71
|
+
else
|
|
72
|
+
@group_excluded = 1
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# See +begin_group+.
|
|
77
|
+
def begin_line kind
|
|
78
|
+
if @group_excluded
|
|
79
|
+
@group_excluded += 1
|
|
80
|
+
elsif include_group? kind
|
|
81
|
+
super
|
|
82
|
+
else
|
|
83
|
+
@group_excluded = 1
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Take care of re-enabling the delegation of tokens to the output stream
|
|
88
|
+
# if an exluded group has ended.
|
|
89
|
+
def end_group kind
|
|
90
|
+
if @group_excluded
|
|
91
|
+
@group_excluded -= 1
|
|
92
|
+
@group_excluded = false if @group_excluded.zero?
|
|
93
|
+
else
|
|
94
|
+
super
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# See +end_group+.
|
|
99
|
+
def end_line kind
|
|
100
|
+
if @group_excluded
|
|
101
|
+
@group_excluded -= 1
|
|
102
|
+
@group_excluded = false if @group_excluded.zero?
|
|
103
|
+
else
|
|
104
|
+
super
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
end
|
|
111
|
+
end
|