From 73ecd3a5bb292406f855bace622960463b88e076 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 12:10:04 +0200 Subject: [PATCH 01/32] fix: comment --- lib/coderay/scanners/bash.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 4854f52..8d6f5d7 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -71,8 +71,10 @@ def scan_tokens tokens, options if @state == :initial if match = scan(/\A#!.*/) kind = :directive - elsif match = scan(/#.*/) + elsif match = scan(/\s*#.*/) kind = :comment + elsif match = scan(/.#/) + kind = :ident elsif match = scan(/(?:\. |source ).*/) kind = :reserved elsif match = scan(/(?:\\.|,)/) From 6baf5cd1dab79bd7f63a896dc2d38a63b7fae655 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 12:43:34 +0200 Subject: [PATCH 02/32] make it work using old api Conflicts: lib/coderay/scanners/bash.rb --- lib/coderay/scanners/bash.rb | 15 ++++++++++----- lib/coderay/scanners/erb_bash.rb | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 8d6f5d7..2bc3f16 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -1,7 +1,8 @@ # Scanner for Bash # Author: Petr Kovar -module CodeRay -module Scanners + +module CodeRay module Scanners + class Bash < Scanner register_for :bash @@ -84,11 +85,15 @@ def scan_tokens tokens, options elsif match = scan(/"/) @state = :quote @quote = match - tokens << [:open, :string] + tokens.begin_group :string tokens << [match, :delimiter] next elsif match = scan(/`/) - tokens << [@shell ? :close : :open, :shell] + if @shell + tokens.end_group :shell + else + tokens.begin_group :shell + end @shell = (not @shell) tokens << [match, :delimiter] next @@ -165,7 +170,7 @@ def scan_tokens tokens, options kind = :content elsif match = scan(/#{@quote}/) tokens << [match, :delimiter] - tokens << [:close, :string] if @quote == '"' + tokens.end_group :string @quote = nil @state = :initial next diff --git a/lib/coderay/scanners/erb_bash.rb b/lib/coderay/scanners/erb_bash.rb index f3adefb..7e05eb2 100644 --- a/lib/coderay/scanners/erb_bash.rb +++ b/lib/coderay/scanners/erb_bash.rb @@ -1,10 +1,10 @@ # Scanner for Bash # Author: spam+github@codez.ch -require 'coderay/scanners/rhtml' +require 'coderay/scanners/erb' module CodeRay module Scanners - class ErbBash < RHTML + class ErbBash < ERB register_for :erb_bash protected From afb822f8c0902657887a0ef06a70e64481954386 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 12:51:28 +0200 Subject: [PATCH 03/32] update gemspec --- coderay_bash.gemspec | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index 01aaf0c..ee88bb0 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -13,17 +13,17 @@ spec = Gem::Specification.new do |s| s.email = "pejuko@gmail.com" s.authors = ["Petr Kovar"] s.name = 'coderay_bash' - s.version = '0.2.1' + s.version = '1.0' s.date = Time.now.strftime("%Y-%m-%d") - s.add_dependency('coderay', '< 1.0') + s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' s.files = ["README.md", "coderay_bash.gemspec", "Rakefile"] s.files += Dir["lib/**/*.rb"] s.post_install_message = <= 1.0. +Bash highlighting for coderay. This gem was tested with coderay 1.0 and won't work with coderay < 1.0. EOF end From 36f96a7fe12eae00b0534ac2e6530e2375c82947 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 13:07:01 +0200 Subject: [PATCH 04/32] update api to 1.0 --- lib/coderay/scanners/bash.rb | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 2bc3f16..84af32b 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -59,13 +59,13 @@ def initialize(*args) @shell = false end - def scan_tokens tokens, options + def scan_tokens encoder, options until eos? kind = match = nil if match = scan(/\n/) - tokens << [match, :end_line] + encoder.text_token(match, :end_line) next end @@ -85,17 +85,17 @@ def scan_tokens tokens, options elsif match = scan(/"/) @state = :quote @quote = match - tokens.begin_group :string - tokens << [match, :delimiter] + encoder.begin_group :string + encoder.text_token(match, :delimiter) next elsif match = scan(/`/) if @shell - tokens.end_group :shell + encoder.end_group :shell else - tokens.begin_group :shell + encoder.begin_group :shell end @shell = (not @shell) - tokens << [match, :delimiter] + encoder.text_token(match, :delimiter) next elsif match = scan(/'[^']*'/) kind = :string @@ -127,8 +127,8 @@ def scan_tokens tokens, options if str.to_s.strip.empty? kind = IDENT_KIND[pre] kind = :instance_variable if kind == :ident - tokens << [pre, kind] - tokens << [op, :operator] + encoder.text_token(pre, kind) + encoder.text_token(op, :operator) next end elsif match = scan(/[A-Za-z_]+\[[A-Za-z_\d]+\]/) @@ -141,9 +141,9 @@ def scan_tokens tokens, options kind = :instance_variable if kind == :ident elsif match = scan(/read \S+/) match =~ /read(\s+)(\S+)/ - tokens << ['read', :method] - tokens << [$1, :space] - tokens << [$2, :instance_variable] + encoder.text_token('read', :method) + encoder.text_token($1, :space) + encoder.text_token($2, :instance_variable) next elsif match = scan(/[\!\:\[\]\{\}]/) kind = :reserved @@ -151,8 +151,8 @@ def scan_tokens tokens, options match =~ /([^;]+);?/ kind = IDENT_KIND[$1] if match[/([^;]+);$/] - tokens << [$1, kind] - tokens << [';', :delimiter] + encoder.text_token($1, kind) + encoder.text_token(';', :delimiter) next end elsif match = scan(/(?: = | - | \+ | \{ | \} | \( | \) | && | \|\| | ;; | ! )/ox) @@ -169,8 +169,8 @@ def scan_tokens tokens, options if (match = scan(/\\.?/)) kind = :content elsif match = scan(/#{@quote}/) - tokens << [match, :delimiter] - tokens.end_group :string + encoder.text_token(match, :delimiter) + encoder.end_group :string @quote = nil @state = :initial next @@ -193,10 +193,10 @@ def scan_tokens tokens, options end match ||= matched - tokens << [match, kind] + encoder.text_token(match, kind) end - tokens + encoder end From 916592b8bd9f32e148431563cc95b248dbfbee4b Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 13:26:10 +0200 Subject: [PATCH 05/32] highlight external programs lik kill, sed, grep, ... --- lib/coderay/scanners/bash.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 84af32b..4fb5f73 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -23,6 +23,14 @@ class Bash < Scanner local logout printf read set shopt source type typeset ulimit unalias ) + PROGRAMS = %w( + awk bash bunzip2 bzcat bzip2 cat chgrp chmod chown cp cut date dd df dir dmesg du ed egrep + false fgrep findmnt fusermount gawk grep groups gunzip gzip hostname install keyctl kill less + ln loadkeys login ls lsblk lsinitcpio lsmod mbchk mkdir mkfifo mknod more mount mountpoint mv + netstat pidof ping ping6 ps pwd readlink red rm rmdir sed sh shred sleep stty su sudo sync tar + touch tput tr traceroute traceroute6 true umount uname uncompress vdir zcat + ) + VARIABLES = %w( CDPATH HOME IFS MAIL MAILPATH OPTARG OPTIND PATH PS1 PS2 ) @@ -47,6 +55,7 @@ class Bash < Scanner add(RESERVED_WORDS, :reserved). add(COMMANDS, :method). add(BASH_COMMANDS, :method). + add(PROGRAMS, :method). add(VARIABLES, :pre_type). add(BASH_VARIABLES, :pre_type) From 1715c4768b36dd825c28653b9331a9f4df0a47dc Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 13:43:05 +0200 Subject: [PATCH 06/32] update erb tests --- test/test.rb | 103 ++++----------------------------------------------- 1 file changed, 7 insertions(+), 96 deletions(-) diff --git a/test/test.rb b/test/test.rb index a790a18..2c0feb8 100644 --- a/test/test.rb +++ b/test/test.rb @@ -10,112 +10,23 @@ $current_dir = File.expand_path(File.dirname(__FILE__)) $:.unshift File.expand_path(File.join($current_dir, "..")) -gem "coderay", "<1.0" require 'lib/coderay_bash' class TestErbBash < Test::Unit::TestCase def test_0010_ErbBash eb_file = File.join($current_dir, "erb_bash.sh") assert_equal( - [["#!/bin/sh", :directive], - ["\n", :end_line], - ["\n", :end_line], - ["perl", :ident], - [" ", :space], - ["-", :operator], - ["e", :ident], - ["'s/to=5/to=10/'", :string], - [" ", :space], - ["/", :plain], - ["test", :method], - ["/", :plain], - ["file", :ident], - ["\n", :end_line], - ["echo", :method], - [" ", :space], - [:open, :string], - ["\"", :delimiter], - ["Parsed at ", :content], - [:open, :inline], - ["<%=", :inline_delimiter], - [" ", :space], - ["Time", :constant], - [".", :operator], - ["now", :ident], - [" ", :space], - ["%>", :inline_delimiter], - [:close, :inline], - ["\"", :delimiter], - [:close, :string], - ["\n", :end_line], - ["echo", :method], - [" ", :space], - [:open, :string], - ["\"", :delimiter], - ["Executed at `Date`", :content], - ["\"", :delimiter], - [:close, :string], - ["\n", :end_line], - ["command", :method], - [" ", :space], - [">>>>>'open quote<<<<<", :error], - ["\n", :end_line], - ["other_command", :ident], - ["\n", :end_line] - ], - CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false)) + ["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, ">>>>>'open quote<<<<<", :error, "\n", :end_line, "other_command", :ident, "\n", :end_line], + CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false).tokens + ) end def test_0011_ErbBash_Ignoring_Errors eb_file = File.join($current_dir, "erb_bash.sh") - assert_equal( - [["#!/bin/sh", :directive], - ["\n", :end_line], - ["\n", :end_line], - ["perl", :ident], - [" ", :space], - ["-", :operator], - ["e", :ident], - ["'s/to=5/to=10/'", :string], - [" ", :space], - ["/", :plain], - ["test", :method], - ["/", :plain], - ["file", :ident], - ["\n", :end_line], - ["echo", :method], - [" ", :space], - [:open, :string], - ["\"", :delimiter], - ["Parsed at ", :content], - [:open, :inline], - ["<%=", :inline_delimiter], - [" ", :space], - ["Time", :constant], - [".", :operator], - ["now", :ident], - [" ", :space], - ["%>", :inline_delimiter], - [:close, :inline], - ["\"", :delimiter], - [:close, :string], - ["\n", :end_line], - ["echo", :method], - [" ", :space], - [:open, :string], - ["\"", :delimiter], - ["Executed at `Date`", :content], - ["\"", :delimiter], - [:close, :string], - ["\n", :end_line], - ["command", :method], - [" ", :space], - ["'open quote", :plain], - ["\n", :end_line], - ["other_command", :ident], - ["\n", :end_line] - ], - CodeRay.scan(File.read(eb_file), :erb_bash)) + assert_equal( + ["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, "'open quote", :plain, "\n", :end_line, "other_command", :ident, "\n", :end_line], + CodeRay.scan(File.read(eb_file), :erb_bash).tokens + ) end end From 40f7e82ea0106b8e271561442f5f06e1610428b2 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 14:07:21 +0200 Subject: [PATCH 07/32] predefined variables and constants --- lib/coderay/scanners/bash.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 4fb5f73..cd995a5 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -56,8 +56,8 @@ class Bash < Scanner add(COMMANDS, :method). add(BASH_COMMANDS, :method). add(PROGRAMS, :method). - add(VARIABLES, :pre_type). - add(BASH_VARIABLES, :pre_type) + add(VARIABLES, :predefined). + add(BASH_VARIABLES, :predefined) attr_reader :state, :quote @@ -126,7 +126,7 @@ def scan_tokens encoder, options elsif match = scan(/ \$\( [^\)]+ \) /ox) kind = :shell elsif match = scan(PRE_CONSTANTS) - kind = :pre_constant + kind = :predefined_constant elsif match = scan(/[^\s'"]*[A-Za-z_][A-Za-z_0-9]*\+?=/) match =~ /(.*?)([A-Za-z_][A-Za-z_0-9]*)(\+?=)/ str = $1 @@ -185,7 +185,7 @@ def scan_tokens encoder, options next #kind = :symbol elsif match = scan(PRE_CONSTANTS) - kind = :pre_constant + kind = :predefined_constant elsif match = scan(/ (?: \$\(\(.*?\)\) ) /x) kind = :global_variable elsif match = scan(/ \$\{?[A-Za-z_][A-Za-z_\d]*\}? /x) From 3fe8e711d38d1e460a9a86cfa1c44c9e30210286 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 14:55:38 +0200 Subject: [PATCH 08/32] support for arrays --- lib/coderay/scanners/bash.rb | 39 +++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index cd995a5..91610f4 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -121,7 +121,14 @@ def scan_tokens encoder, options kind = :global_variable elsif match = scan(/ \$\{ [^\}]+ \} /ox) match =~ /\$\{(.*)\}/ - kind = IDENT_KIND[$1] + var=$1 + if var =~ /\[.*\]/ + encoder.text_token("${", :instance_variable) + match_array(var, encoder) + encoder.text_token("}", :instance_variable) + next + end + kind = IDENT_KIND[var] kind = :instance_variable if kind == :ident elsif match = scan(/ \$\( [^\)]+ \) /ox) kind = :shell @@ -140,10 +147,10 @@ def scan_tokens encoder, options encoder.text_token(op, :operator) next end - elsif match = scan(/[A-Za-z_]+\[[A-Za-z_\d]+\]/) + elsif match = scan(/[A-Za-z_]+\[[A-Za-z_\@\*\d]+\]/) # array - kind = IDENT_KIND(match) - kind = :instance_variable if kind == :ident + match_array(match, encoder) + next elsif match = scan(/ \$[A-Za-z_][A-Za-z_0-9]* /ox) match =~ /\$(.*)/ kind = IDENT_KIND[$1] @@ -188,7 +195,17 @@ def scan_tokens encoder, options kind = :predefined_constant elsif match = scan(/ (?: \$\(\(.*?\)\) ) /x) kind = :global_variable - elsif match = scan(/ \$\{?[A-Za-z_][A-Za-z_\d]*\}? /x) + elsif match = scan(/ \$ (?: (?: \{ [^\}]* \}) | (?: [A-Za-z_0-9]+ ) ) /x) + match =~ /(\$\{?)([^\}]*)(\}?)/ + pre=$1 + var=$2 + post=$3 + if var =~ /\[.*?\]/ + encoder.text_token(pre,:instance_variable) + match_array(var, encoder) + encoder.text_token(post,:instance_variable) + next + end kind = IDENT_KIND[match] kind = :instance_variable if kind == :ident elsif match = scan(/[^#{@quote}\\]+/) @@ -208,6 +225,18 @@ def scan_tokens encoder, options encoder end + + def match_array(match, encoder) + match =~ /([A-Za-z_]+)\[(.*?)\]/ + var = $1 + key = $2 + kind = IDENT_KIND[var] + kind = :instance_variable if kind == :ident + encoder.text_token(var, kind) + encoder.text_token("[", :operator) + encoder.text_token(key, :key) + encoder.text_token("]", :operator) + end def handle_error(match, options) o = {:ignore_errors => true}.merge(options) From 148b76789b2e876c56892fb1f482c788b2a234cc Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 15:11:13 +0200 Subject: [PATCH 09/32] changelog --- CHANGELOG | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index e77a0a2..377a084 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,12 @@ +v1.0 +---- + +* compatible with coderay 1.0 +* `` doesn't change state but sets @shell variable to true +* support for arrays +* fix comments + + v0.2.1 ------ From 621c01a04dee8bab9f99636f0b2edc336ae8d509 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Tue, 4 Oct 2011 15:32:15 +0200 Subject: [PATCH 10/32] switch off program highlighting --- lib/coderay/scanners/bash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 91610f4..964aef2 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -55,7 +55,7 @@ class Bash < Scanner add(RESERVED_WORDS, :reserved). add(COMMANDS, :method). add(BASH_COMMANDS, :method). - add(PROGRAMS, :method). +# add(PROGRAMS, :method). add(VARIABLES, :predefined). add(BASH_VARIABLES, :predefined) From 9f9a3bc8d567174c046ca3ae79fdd2bef6d0a8dc Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 11 Mar 2012 16:01:51 +0100 Subject: [PATCH 11/32] fix: open single quote --- lib/coderay/scanners/bash.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 964aef2..76cb23f 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -106,7 +106,7 @@ def scan_tokens encoder, options @shell = (not @shell) encoder.text_token(match, :delimiter) next - elsif match = scan(/'[^']*'/) + elsif match = scan(/'[^']*'?/) kind = :string elsif match = scan(/(?: \& | > | < | \| >> | << | >\& )/ox) kind = :bin @@ -222,6 +222,10 @@ def scan_tokens encoder, options encoder.text_token(match, kind) end + if @state == :quote + encoder.end_group :string + end + encoder end From c87705ce7d148ed7481f80082448873109b9e211 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 11 Mar 2012 16:16:13 +0100 Subject: [PATCH 12/32] support for heredoc --- lib/coderay/scanners/bash.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 76cb23f..94fc072 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -97,6 +97,13 @@ def scan_tokens encoder, options encoder.begin_group :string encoder.text_token(match, :delimiter) next + elsif match = scan(/<<\S+/) + @state = :quote + match =~ /<<(\S+)/ + @quote = "#{$1}" + encoder.begin_group :string + encoder.text_token(match, :delimiter) + next elsif match = scan(/`/) if @shell encoder.end_group :shell From 27eab4e699e33a030628d2b8a18a04193df7f4c5 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 11 Mar 2012 16:21:56 +0100 Subject: [PATCH 13/32] set version 1.0.1 --- CHANGELOG | 7 +++++++ coderay_bash.gemspec | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 377a084..d5eb48e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +v1.0.1 +------ + +* fix open single quote +* support for heredoc + + v1.0 ---- diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index ee88bb0..10b03b9 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s| s.email = "pejuko@gmail.com" s.authors = ["Petr Kovar"] s.name = 'coderay_bash' - s.version = '1.0' + s.version = '1.0.1' s.date = Time.now.strftime("%Y-%m-%d") s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' From b6577f89ce48708316321d642c5063a579f04c34 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 11 Mar 2012 16:35:30 +0100 Subject: [PATCH 14/32] heredoc tests --- test/heredoc.sh | 7 +++++++ test/test.rb | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/heredoc.sh diff --git a/test/heredoc.sh b/test/heredoc.sh new file mode 100644 index 0000000..782a1cd --- /dev/null +++ b/test/heredoc.sh @@ -0,0 +1,7 @@ +#/bin/bash + +cat <", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, ">>>>>'open quote<<<<<", :error, "\n", :end_line, "other_command", :ident, "\n", :end_line], + #["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, ">>>>>'open quote<<<<<", :error, "\n", :end_line, "other_command", :ident, "\n", :end_line], + ["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :end_group, :string, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, "'open quote\nother_command\n", :string], CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false).tokens ) end @@ -24,9 +25,18 @@ def test_0010_ErbBash def test_0011_ErbBash_Ignoring_Errors eb_file = File.join($current_dir, "erb_bash.sh") assert_equal( - ["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, "'open quote", :plain, "\n", :end_line, "other_command", :ident, "\n", :end_line], + #["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, "'open quote", :plain, "\n", :end_line, "other_command", :ident, "\n", :end_line], + ["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :end_group, :string, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, "'open quote\nother_command\n", :string], CodeRay.scan(File.read(eb_file), :erb_bash).tokens ) end + def test_0011_ErbBash_Ignoring_Errors + file = File.join($current_dir, "heredoc.sh") + assert_equal( + ["#/bin/bash", :comment, "\n", :end_line, "\n", :end_line, "cat", :ident, " ", :space, :begin_group, :string, "< Date: Wed, 14 Mar 2012 08:00:24 +0100 Subject: [PATCH 15/32] fix: nested shells --- lib/coderay/scanners/bash.rb | 36 ++++++++++++++++++++++++++++++++---- test/nested_shells.sh | 1 + test/test.rb | 12 ++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 test/nested_shells.sh diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 94fc072..0e516d6 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -66,6 +66,8 @@ def initialize(*args) @state = :initial @quote = nil @shell = false + @brace_shell = 0 + @quote_brace_shell = 0 end def scan_tokens encoder, options @@ -106,12 +108,13 @@ def scan_tokens encoder, options next elsif match = scan(/`/) if @shell + encoder.text_token(match, :delimiter) encoder.end_group :shell else encoder.begin_group :shell + encoder.text_token(match, :delimiter) end @shell = (not @shell) - encoder.text_token(match, :delimiter) next elsif match = scan(/'[^']*'?/) kind = :string @@ -137,8 +140,19 @@ def scan_tokens encoder, options end kind = IDENT_KIND[var] kind = :instance_variable if kind == :ident - elsif match = scan(/ \$\( [^\)]+ \) /ox) - kind = :shell + #elsif match = scan(/ \$\( [^\)]+ \) /ox) + elsif match = scan(/ \$\( /ox) + @brace_shell += 1 + encoder.begin_group :shell + encoder.text_token(match, :delimiter) + next + elsif match = scan(/ \) /ox) + if @brace_shell > 0 + encoder.text_token(match, :delimiter) + encoder.end_group :shell + @brace_shell -= 1 + next + end elsif match = scan(PRE_CONSTANTS) kind = :predefined_constant elsif match = scan(/[^\s'"]*[A-Za-z_][A-Za-z_0-9]*\+?=/) @@ -202,6 +216,20 @@ def scan_tokens encoder, options kind = :predefined_constant elsif match = scan(/ (?: \$\(\(.*?\)\) ) /x) kind = :global_variable + elsif match = scan(/ \$\( /ox) + encoder.begin_group :shell + encoder.text_token(match, :delimiter) + @quote_brace_shell += 1 + next + elsif match = scan(/\)/) + if @quote_brace_shell > 0 + encoder.text_token(match, :delimiter) + encoder.end_group :shell + @quote_brace_shell -= 1 + next + else + kind = :content + end elsif match = scan(/ \$ (?: (?: \{ [^\}]* \}) | (?: [A-Za-z_0-9]+ ) ) /x) match =~ /(\$\{?)([^\}]*)(\}?)/ pre=$1 @@ -215,7 +243,7 @@ def scan_tokens encoder, options end kind = IDENT_KIND[match] kind = :instance_variable if kind == :ident - elsif match = scan(/[^#{@quote}\\]+/) + elsif match = scan(/[^\)\$#{@quote}\\]+/) kind = :content else match = scan(/.+/) # this shouldn't be diff --git a/test/nested_shells.sh b/test/nested_shells.sh new file mode 100644 index 0000000..ba742d9 --- /dev/null +++ b/test/nested_shells.sh @@ -0,0 +1 @@ +blueberry=$(date -d "$(stat -c $(%z) blueberry.exe)") diff --git a/test/test.rb b/test/test.rb index 026882a..649dfea 100644 --- a/test/test.rb +++ b/test/test.rb @@ -22,7 +22,7 @@ def test_0010_ErbBash ) end - def test_0011_ErbBash_Ignoring_Errors + def test_0020_ErbBash_Ignoring_Errors eb_file = File.join($current_dir, "erb_bash.sh") assert_equal( #["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "perl", :ident, " ", :space, "-", :operator, "e", :ident, "'s/to=5/to=10/'", :string, " ", :space, "/", :plain, "test", :method, "/", :plain, "file", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Parsed at ", :content, :begin_group, :inline, "<%=", :inline_delimiter, " ", :space, "Time", :constant, ".", :operator, "now", :ident, " ", :space, "%>", :inline_delimiter, :end_group, :inline, "\"", :delimiter, :end_group, :string, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "Executed at `Date`", :content, "\"", :delimiter, :end_group, :string, "\n", :end_line, "command", :method, " ", :space, "'open quote", :plain, "\n", :end_line, "other_command", :ident, "\n", :end_line], @@ -31,7 +31,7 @@ def test_0011_ErbBash_Ignoring_Errors ) end - def test_0011_ErbBash_Ignoring_Errors + def test_0030_heredoc file = File.join($current_dir, "heredoc.sh") assert_equal( ["#/bin/bash", :comment, "\n", :end_line, "\n", :end_line, "cat", :ident, " ", :space, :begin_group, :string, "< Date: Wed, 14 Mar 2012 08:00:58 +0100 Subject: [PATCH 16/32] bounce version --- coderay_bash.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index 10b03b9..d0a8330 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s| s.email = "pejuko@gmail.com" s.authors = ["Petr Kovar"] s.name = 'coderay_bash' - s.version = '1.0.1' + s.version = '1.0.2' s.date = Time.now.strftime("%Y-%m-%d") s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' From 3b290ddbc3b78bd19e8239c717c461adc4afb4ad Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Wed, 14 Mar 2012 08:09:43 +0100 Subject: [PATCH 17/32] changelog --- CHANGELOG | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index d5eb48e..fd07add 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,9 @@ +v1.0.2 +------ + +* fix nested shells + + v1.0.1 ------ From 421953cad094dc59e3dc328fb2afe86ee3a31069 Mon Sep 17 00:00:00 2001 From: Pascal Zumkehr Date: Fri, 31 Aug 2012 16:58:57 +0200 Subject: [PATCH 18/32] fix function brackets error --- lib/coderay/scanners/bash.rb | 12 +++++------- test/function.sh | 6 ++++++ test/test.rb | 9 ++++++++- 3 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 test/function.sh diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 0e516d6..efad3c2 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -146,13 +146,11 @@ def scan_tokens encoder, options encoder.begin_group :shell encoder.text_token(match, :delimiter) next - elsif match = scan(/ \) /ox) - if @brace_shell > 0 - encoder.text_token(match, :delimiter) - encoder.end_group :shell - @brace_shell -= 1 - next - end + elsif @brace_shell > 0 && match = scan(/ \) /ox) + encoder.text_token(match, :delimiter) + encoder.end_group :shell + @brace_shell -= 1 + next elsif match = scan(PRE_CONSTANTS) kind = :predefined_constant elsif match = scan(/[^\s'"]*[A-Za-z_][A-Za-z_0-9]*\+?=/) diff --git a/test/function.sh b/test/function.sh new file mode 100644 index 0000000..f719615 --- /dev/null +++ b/test/function.sh @@ -0,0 +1,6 @@ +#/bin/bash + +function bla() +{ + other_command +} \ No newline at end of file diff --git a/test/test.rb b/test/test.rb index 649dfea..a5b1efd 100644 --- a/test/test.rb +++ b/test/test.rb @@ -30,7 +30,14 @@ def test_0020_ErbBash_Ignoring_Errors CodeRay.scan(File.read(eb_file), :erb_bash).tokens ) end - + + def test_0010_ErbBash_to_html + eb_file = File.join($current_dir, "function.sh") + assert_nothing_raised { + CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false).html + } + end + def test_0030_heredoc file = File.join($current_dir, "heredoc.sh") assert_equal( From a4599f5582c8ed75cf73b2b475c8c5c71f8f43b6 Mon Sep 17 00:00:00 2001 From: Pascal Zumkehr Date: Fri, 31 Aug 2012 17:15:24 +0200 Subject: [PATCH 19/32] fix strings starting with comment character --- lib/coderay/scanners/bash.rb | 2 +- test/string_comment.sh | 2 ++ test/test.rb | 24 ++++++++++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 test/string_comment.sh diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index efad3c2..14d0c00 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -85,7 +85,7 @@ def scan_tokens encoder, options kind = :directive elsif match = scan(/\s*#.*/) kind = :comment - elsif match = scan(/.#/) + elsif match = scan(/[^"]#/) kind = :ident elsif match = scan(/(?:\. |source ).*/) kind = :reserved diff --git a/test/string_comment.sh b/test/string_comment.sh new file mode 100644 index 0000000..a3f6b17 --- /dev/null +++ b/test/string_comment.sh @@ -0,0 +1,2 @@ +echo "#output a comment" > foo +echo "more" >> foo # so this is a comment now diff --git a/test/test.rb b/test/test.rb index a5b1efd..53b0c1c 100644 --- a/test/test.rb +++ b/test/test.rb @@ -30,14 +30,7 @@ def test_0020_ErbBash_Ignoring_Errors CodeRay.scan(File.read(eb_file), :erb_bash).tokens ) end - - def test_0010_ErbBash_to_html - eb_file = File.join($current_dir, "function.sh") - assert_nothing_raised { - CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false).html - } - end - + def test_0030_heredoc file = File.join($current_dir, "heredoc.sh") assert_equal( @@ -53,5 +46,20 @@ def test_0040_nested_shell CodeRay.scan(File.read(file), :bash).tokens ) end + + def test_0050_ErbBash_to_html + eb_file = File.join($current_dir, "function.sh") + assert_nothing_raised { + CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false).html + } + end + + def test_0060_Comments_in_strings + eb_file = File.join($current_dir, "string_comment.sh") + assert_equal( + ["echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "#output a comment", :content, "\"", :delimiter, :end_group, :string, " ", :space, ">", :bin, " ", :space, "foo", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "more", :content, "\"", :delimiter, :end_group, :string, " ", :space, ">", :bin, ">", :bin, " ", :space, "foo", :ident, " # so this is a comment now", :comment, "\n", :end_line], + CodeRay.scan(File.read(eb_file), :bash).tokens + ) + end end From f897d9989c988b515adfc1decdb8ddc1db8f03d0 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 2 Sep 2012 18:13:18 +0200 Subject: [PATCH 20/32] 1.0.3 release --- CHANGELOG | 7 +++++++ coderay_bash.gemspec | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index fd07add..9ce403a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +v1.0.3 +------ + +* fix function closing bracket (by codez) +* fix # in string is not a comment (by codez) + + v1.0.2 ------ diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index d0a8330..ba359d7 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s| s.email = "pejuko@gmail.com" s.authors = ["Petr Kovar"] s.name = 'coderay_bash' - s.version = '1.0.2' + s.version = '1.0.3' s.date = Time.now.strftime("%Y-%m-%d") s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' From 50f06311a973893a4dcae69814ff720ed14894d2 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 4 Nov 2012 13:04:54 +0100 Subject: [PATCH 21/32] fix issue #10 array names can now contain also numbers and other chars to prevent scanner issue errors --- lib/coderay/scanners/bash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 14d0c00..39a93d6 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -264,7 +264,7 @@ def scan_tokens encoder, options def match_array(match, encoder) - match =~ /([A-Za-z_]+)\[(.*?)\]/ + match =~ /(.+)\[(.*?)\]/ var = $1 key = $2 kind = IDENT_KIND[var] From 1be6b65bacd9070dd39c93908f59116360d21439 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 4 Nov 2012 13:07:41 +0100 Subject: [PATCH 22/32] refactor tests create more classes for test and add test case for arrays including number in the name --- test/test.rb | 55 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/test/test.rb b/test/test.rb index 53b0c1c..e495c2e 100644 --- a/test/test.rb +++ b/test/test.rb @@ -12,7 +12,9 @@ require 'lib/coderay_bash' + class TestErbBash < Test::Unit::TestCase + def test_0010_ErbBash eb_file = File.join($current_dir, "erb_bash.sh") assert_equal( @@ -30,8 +32,20 @@ def test_0020_ErbBash_Ignoring_Errors CodeRay.scan(File.read(eb_file), :erb_bash).tokens ) end - - def test_0030_heredoc + + def test_0030_ErbBash_to_html + eb_file = File.join($current_dir, "function.sh") + assert_nothing_raised { + CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false).html + } + end + +end + + +class TestString < Test::Unit::TestCase + + def test_0010_heredoc file = File.join($current_dir, "heredoc.sh") assert_equal( ["#/bin/bash", :comment, "\n", :end_line, "\n", :end_line, "cat", :ident, " ", :space, :begin_group, :string, "<", :bin, " ", :space, "foo", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "more", :content, "\"", :delimiter, :end_group, :string, " ", :space, ">", :bin, ">", :bin, " ", :space, "foo", :ident, " # so this is a comment now", :comment, "\n", :end_line], + CodeRay.scan(File.read(eb_file), :bash).tokens + ) + end + +end + + +class TestNested < Test::Unit::TestCase + + def test_0010_nested_shell file = File.join($current_dir, "nested_shells.sh") assert_equal( ["blueberry", :instance_variable, "=", :operator, :begin_group, :shell, "$(", :delimiter, "date", :ident, " ", :space, "-", :operator, "d", :ident, " ", :space, :begin_group, :string, "\"", :delimiter, :begin_group, :shell, "$(", :delimiter, "stat -c ", :content, :begin_group, :shell, "$(", :delimiter, "%z", :content, ")", :delimiter, :end_group, :shell, " blueberry.exe", :content, ")", :delimiter, :end_group, :shell, "\"", :delimiter, :end_group, :string, ")", :delimiter, :end_group, :shell, "\n", :end_line], CodeRay.scan(File.read(file), :bash).tokens ) end + +end + + +class TestArray < Test::Unit::TestCase - def test_0050_ErbBash_to_html - eb_file = File.join($current_dir, "function.sh") - assert_nothing_raised { - CodeRay.scan(File.read(eb_file), :erb_bash, :ignore_errors => false).html - } - end - - def test_0060_Comments_in_strings - eb_file = File.join($current_dir, "string_comment.sh") - assert_equal( - ["echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "#output a comment", :content, "\"", :delimiter, :end_group, :string, " ", :space, ">", :bin, " ", :space, "foo", :ident, "\n", :end_line, "echo", :method, " ", :space, :begin_group, :string, "\"", :delimiter, "more", :content, "\"", :delimiter, :end_group, :string, " ", :space, ">", :bin, ">", :bin, " ", :space, "foo", :ident, " # so this is a comment now", :comment, "\n", :end_line], - CodeRay.scan(File.read(eb_file), :bash).tokens - ) + def test_0010_Array + eb_file = File.join($current_dir, "json.sh") + assert_equal(["cat",:ident," ",:space,"$1",:predefined_constant," ",:space,"|",:plain," ",:space,"while",:reserved," ",:space,"read",:method," ",:space,"json;",:instance_variable," ",:space,"do",:reserved," ",:space,"if",:reserved," ",:space,"[",:reserved,"[",:reserved," ",:space,"${",:instance_variable,"array2",:instance_variable,"[",:operator,"0",:key,"]",:operator,"}",:instance_variable," ",:space,"=",:operator,"=",:operator," ",:space,"1",:integer," ",:space,"]",:reserved,"]",:reserved,";",:delimiter," ",:space,"then",:reserved,"\n",:end_line,"echo",:method," ",:space,"$json",:instance_variable," ",:space,"|",:plain," ",:space,".",:plain,".",:plain,".",:plain,".",:plain,".",:plain,".",:plain,"\n",:end_line], + CodeRay.scan(File.read(eb_file), :bash).tokens) end end From 2649e2834df629934b0049407f1bde207dc7545a Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 4 Nov 2012 14:05:36 +0100 Subject: [PATCH 23/32] issue #7: source fixed --- lib/coderay/scanners/bash.rb | 4 +++- test/test.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index 39a93d6..eb03cbe 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -87,7 +87,9 @@ def scan_tokens encoder, options kind = :comment elsif match = scan(/[^"]#/) kind = :ident - elsif match = scan(/(?:\. |source ).*/) + elsif match = scan(/\.+/) + kind = :plain + elsif match = scan(/(?:\.|source)\s+/) kind = :reserved elsif match = scan(/(?:\\.|,)/) kind = :plain diff --git a/test/test.rb b/test/test.rb index e495c2e..4a0ce93 100644 --- a/test/test.rb +++ b/test/test.rb @@ -81,7 +81,7 @@ class TestArray < Test::Unit::TestCase def test_0010_Array eb_file = File.join($current_dir, "json.sh") - assert_equal(["cat",:ident," ",:space,"$1",:predefined_constant," ",:space,"|",:plain," ",:space,"while",:reserved," ",:space,"read",:method," ",:space,"json;",:instance_variable," ",:space,"do",:reserved," ",:space,"if",:reserved," ",:space,"[",:reserved,"[",:reserved," ",:space,"${",:instance_variable,"array2",:instance_variable,"[",:operator,"0",:key,"]",:operator,"}",:instance_variable," ",:space,"=",:operator,"=",:operator," ",:space,"1",:integer," ",:space,"]",:reserved,"]",:reserved,";",:delimiter," ",:space,"then",:reserved,"\n",:end_line,"echo",:method," ",:space,"$json",:instance_variable," ",:space,"|",:plain," ",:space,".",:plain,".",:plain,".",:plain,".",:plain,".",:plain,".",:plain,"\n",:end_line], + assert_equal(["cat",:ident," ",:space,"$1",:predefined_constant," ",:space,"|",:plain," ",:space,"while",:reserved," ",:space,"read",:method," ",:space,"json;",:instance_variable," ",:space,"do",:reserved," ",:space,"if",:reserved," ",:space,"[",:reserved,"[",:reserved," ",:space,"${",:instance_variable,"array2",:instance_variable,"[",:operator,"0",:key,"]",:operator,"}",:instance_variable," ",:space,"=",:operator,"=",:operator," ",:space,"1",:integer," ",:space,"]",:reserved,"]",:reserved,";",:delimiter," ",:space,"then",:reserved,"\n",:end_line,"echo",:method," ",:space,"$json",:instance_variable," ",:space,"|",:plain," ",:space,"......",:plain,"\n",:end_line], CodeRay.scan(File.read(eb_file), :bash).tokens) end From 183aa1e75d1a32477da3b1663cf9956074610d69 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 4 Nov 2012 14:12:07 +0100 Subject: [PATCH 24/32] fix '.' --- lib/coderay/scanners/bash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index eb03cbe..bf87a5e 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -87,7 +87,7 @@ def scan_tokens encoder, options kind = :comment elsif match = scan(/[^"]#/) kind = :ident - elsif match = scan(/\.+/) + elsif match = scan(/\.\.+/) kind = :plain elsif match = scan(/(?:\.|source)\s+/) kind = :reserved From 7467340a6268a9a6661f0cf104ff71d188b56824 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 4 Nov 2012 14:18:33 +0100 Subject: [PATCH 25/32] version 1.0.4 --- coderay_bash.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index ba359d7..809f3e7 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s| s.email = "pejuko@gmail.com" s.authors = ["Petr Kovar"] s.name = 'coderay_bash' - s.version = '1.0.3' + s.version = '1.0.4' s.date = Time.now.strftime("%Y-%m-%d") s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' From 7c4aa0f735fc78bcc599a3badb425b790ff34e26 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sun, 4 Nov 2012 14:22:24 +0100 Subject: [PATCH 26/32] changelog --- CHANGELOG | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 9ce403a..de66b71 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,9 @@ +v1.0.4 +------ + +* fix issue #10 +* fix source + v1.0.3 ------ From 0703318fd635dc609a0b98945f2ec4d95e2e86e8 Mon Sep 17 00:00:00 2001 From: Alexandre Vezina Date: Thu, 14 Mar 2013 12:18:20 -0400 Subject: [PATCH 27/32] Register sh filetype --- lib/coderay_bash.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/coderay_bash.rb b/lib/coderay_bash.rb index 85f515d..5c2d2c0 100644 --- a/lib/coderay_bash.rb +++ b/lib/coderay_bash.rb @@ -4,3 +4,6 @@ require File.join(path, "coderay/scanners/bash.rb") require File.join(path, "coderay/scanners/erb_bash.rb") + +# Register file types +::CodeRay::FileType::TypeFromExt['sh'] = :bash From f59702ad58e05936f35dc1003e5f407649f25e74 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sat, 16 Mar 2013 20:52:20 +0100 Subject: [PATCH 28/32] version v1.0.5 --- CHANGELOG | 5 +++++ coderay_bash.gemspec | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index de66b71..804075c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +v1.0.5 +------ + +* register .sh file extension with :bash scanner (by Alexandre Vezina) + v1.0.4 ------ diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index 809f3e7..e8f8455 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -13,11 +13,11 @@ spec = Gem::Specification.new do |s| s.email = "pejuko@gmail.com" s.authors = ["Petr Kovar"] s.name = 'coderay_bash' - s.version = '1.0.4' + s.version = '1.0.5' s.date = Time.now.strftime("%Y-%m-%d") s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' - s.files = ["README.md", "coderay_bash.gemspec", "Rakefile"] + s.files = ["README.md", "CHANGELOG", "coderay_bash.gemspec", "Rakefile"] s.files += Dir["lib/**/*.rb"] s.post_install_message = < Date: Sat, 20 Sep 2014 13:52:21 +0200 Subject: [PATCH 29/32] MIT License --- CHANGELOG | 5 +++++ MIT_License.txt | 22 ++++++++++++++++++++++ README.md | 4 ++++ Rakefile | 4 ++-- coderay_bash.gemspec | 5 +++-- 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 MIT_License.txt diff --git a/CHANGELOG b/CHANGELOG index 804075c..bcbaf89 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +v1.0.6 +------ + +* license file added + v1.0.5 ------ diff --git a/MIT_License.txt b/MIT_License.txt new file mode 100644 index 0000000..8e15fb5 --- /dev/null +++ b/MIT_License.txt @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2010-2014 Petr Kovar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/README.md b/README.md index bf01a6a..155da34 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,7 @@ Bash scanner for highlighting scripts with CodeRay * `:erb_bash` -- erb code in bash strings For more information see [CodeRay web pages](http://coderay.rubychan.de/) + +### Licence + +MIT diff --git a/Rakefile b/Rakefile index 2dab200..145cf1d 100644 --- a/Rakefile +++ b/Rakefile @@ -4,14 +4,14 @@ # @author: Petr Kovar $KCODE='UTF8' if RUBY_VERSION < "1.9" -require 'rake/gempackagetask' +require 'rubygems/package_task' require 'rake/testtask' require 'rake/clean' CLEAN << "coverage" << "pkg" << "README.html" << "CHANGELOG.html" task :default => [:test, :gem] -Rake::GemPackageTask.new(eval(File.read("coderay_bash.gemspec"))) {|pkg|} +Gem::PackageTask.new(eval(File.read("coderay_bash.gemspec"))) {|pkg|} Rake::TestTask.new(:test) do |t| t.pattern = File.join(File.dirname(__FILE__), 'test/test.rb') diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index e8f8455..ca91b3f 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -12,12 +12,13 @@ spec = Gem::Specification.new do |s| s.homepage = "http://github.com/pejuko/coderay_bash" s.email = "pejuko@gmail.com" s.authors = ["Petr Kovar"] + s.licenses = ["MIT"] s.name = 'coderay_bash' - s.version = '1.0.5' + s.version = '1.0.6' s.date = Time.now.strftime("%Y-%m-%d") s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' - s.files = ["README.md", "CHANGELOG", "coderay_bash.gemspec", "Rakefile"] + s.files = ["MIT_License.txt", "README.md", "CHANGELOG", "coderay_bash.gemspec", "Rakefile"] s.files += Dir["lib/**/*.rb"] s.post_install_message = < Date: Sat, 7 Feb 2015 17:07:30 +0100 Subject: [PATCH 30/32] issue #13 - problem with stripping when two = fixed greedy regexp --- lib/coderay/scanners/bash.rb | 2 +- test/keep_path.sh | 7 +++++++ test/test.rb | 11 +++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/keep_path.sh diff --git a/lib/coderay/scanners/bash.rb b/lib/coderay/scanners/bash.rb index bf87a5e..03783f8 100644 --- a/lib/coderay/scanners/bash.rb +++ b/lib/coderay/scanners/bash.rb @@ -155,7 +155,7 @@ def scan_tokens encoder, options next elsif match = scan(PRE_CONSTANTS) kind = :predefined_constant - elsif match = scan(/[^\s'"]*[A-Za-z_][A-Za-z_0-9]*\+?=/) + elsif match = scan(/[^\s'"]*?[A-Za-z_][A-Za-z_0-9]*\+?=/) match =~ /(.*?)([A-Za-z_][A-Za-z_0-9]*)(\+?=)/ str = $1 pre = $2 diff --git a/test/keep_path.sh b/test/keep_path.sh new file mode 100644 index 0000000..5fa2bec --- /dev/null +++ b/test/keep_path.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +sudo qemu-kvm -m 768 \ + -boot d \ + -drive file=/bak/kvm/salix.qcow,cache=writeback \ + -cdrom /ntfs-d/ISO/salix64-openbox-14.1.iso -vnc :3 + diff --git a/test/test.rb b/test/test.rb index 4a0ce93..3b4e706 100644 --- a/test/test.rb +++ b/test/test.rb @@ -86,3 +86,14 @@ def test_0010_Array end end + + +class TestKeepPath < Test::Unit::TestCase + + def test_0010_Array + file = File.join($current_dir, "keep_path.sh") + assert_equal(["#!/bin/sh", :directive, "\n", :end_line, "\n", :end_line, "sudo", :ident, " ", :space, "qemu", :ident, "-", :operator, "kvm", :ident, " ", :space, "-", :operator, "m", :ident, " ", :space, "768", :integer, " ", :space, "\\", :plain, "\n", :end_line, " ", :space, "-", :operator, "boot", :ident, " ", :space, "d", :ident, " ", :space, "\\", :plain, "\n", :end_line, " ", :space, "-", :operator, "drive", :ident, " ", :space, "file", :instance_variable, "=", :operator, "/bak/kvm/salix.qcow,cache=", :plain, "writeback", :ident, " ", :space, "\\", :plain, "\n", :end_line, " ", :space, "-", :operator, "cdrom", :ident, " ", :space, "/", :plain, "ntfs", :ident, "-", :operator, "d", :ident, "/", :plain, "ISO", :ident, "/", :plain, "salix64", :ident, "-", :operator, "openbox", :ident, "-", :operator, "14.1.", :float, "iso", :ident, " ", :space, "-", :operator, "vnc", :ident, " ", :space, ":", :reserved, "3", :integer, "\n", :end_line, "\n", :end_line], + CodeRay.scan(File.read(file), :bash).tokens) + end + +end From 704844d6345ca56ba658dc7ac5952aab6b1d5a32 Mon Sep 17 00:00:00 2001 From: Petr Kovar Date: Sat, 7 Feb 2015 17:17:45 +0100 Subject: [PATCH 31/32] new gem version 1.0.7 --- coderay_bash.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderay_bash.gemspec b/coderay_bash.gemspec index ca91b3f..1b32840 100644 --- a/coderay_bash.gemspec +++ b/coderay_bash.gemspec @@ -14,7 +14,7 @@ spec = Gem::Specification.new do |s| s.authors = ["Petr Kovar"] s.licenses = ["MIT"] s.name = 'coderay_bash' - s.version = '1.0.6' + s.version = '1.0.7' s.date = Time.now.strftime("%Y-%m-%d") s.add_dependency('coderay', '>= 1.0') s.require_path = 'lib' From edab46deb01f363b386e2a96320e71b42b1ae04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Lipt=C3=A1k?= Date: Sat, 12 Mar 2016 17:46:13 -0500 Subject: [PATCH 32/32] Improve formatting --- README.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 155da34..246e276 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,27 @@ Bash scanner for highlighting scripts with CodeRay ================================================== -### Instalation +### Installation - gem install coderay_bash +```bash +gem install coderay_bash +``` ### Usage - require 'rubygems' - require 'coderay_bash' +```ruby +require 'rubygems' +require 'coderay_bash' - plain = File.read('some_script.sh') - @body = CodeRay.scan(plain, :bash).div +plain = File.read('some_script.sh') +@body = CodeRay.scan(plain, :bash).div +``` ### in your template then do something like - <%= @body %> +```html +<%= @body %> +``` ### Types