diff --git a/app/assets/javascripts/dropdown.coffee b/app/assets/javascripts/dropdown.coffee index ed7b8bb..583103d 100644 --- a/app/assets/javascripts/dropdown.coffee +++ b/app/assets/javascripts/dropdown.coffee @@ -20,6 +20,7 @@ class Selector $el.addClass 'selected' self.placeholder.text "#{self.prefix}: #{$el.text()}" self.setValue $el.data 'value' + $('textarea').focus() @options.filter('.selected').click() @dropdown.removeClass 'active' diff --git a/app/assets/stylesheets/pastemaster.styl b/app/assets/stylesheets/pastemaster.styl index 47c377b..a768df2 100644 --- a/app/assets/stylesheets/pastemaster.styl +++ b/app/assets/stylesheets/pastemaster.styl @@ -1,83 +1,81 @@ -* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; } +* + -moz-box-sizing border-box + -webkit-box-sizing border-box + box-sizing border-box -body, input, select { - font-family: Helvetica, Arial, sans-serif; - font-size: 1em; -} -textarea, pre { - font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif; - font-size: 1em; -} -html, body, form { - height: 100%; -} -body { - background-color: #fcfcfc; - padding: 3em; -} -h1 { - position: absolute; - top: 1em; - left: 3em; - font-weight: 200; - font-size: 1em; - line-height: 1em; - padding: 0; - margin: 0; - text-transform: uppercase; - color: #ccc; -} -.linenos { - padding-right: .5em; - color: #ccc; - border-right: #eee 1px solid; -} -.code { - padding-left: .5em; -} +body, input, select + font-family Helvetica, Arial, sans-serif + font-size 1em -.container { - width: 100%; - height: 100%; -} +textarea, pre + font-family Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif + font-size 1em -textarea, input { - outline: none; -} +html, body, form + height 100% -textarea { - background-color: #ffffff; - width: 100%; - height: 100%; - line-height: 120%; - resize: none; - border: #ddd 1px solid; - border-radius: 5px; - padding: .5em; -} +body + background-color #fcfcfc + padding 3em -form input[type="submit"], form a[role="submit"] { - float: right; -} -form a[role="submit"] { - background-color: #fefefe; - background-image: -webkit-linear-gradient(top, #ffffff, #f5f5f5); - background-image: linear-gradient(to bottom, #ffffff, #f5f5f5); - box-shadow: 0 1px 2px 2px #ffffff; - color: #666; - text-shadow: 0 0 2px #ffffff; - border: #ddd 1px solid; - border-radius: 4px; - border-bottom-width: 4px; - font-size: 1em; - width: 5em; - text-align: center; - padding: .4em 0; - margin: .4em 0; - text-decoration: none; - display: none; -} -form a[role="submit"]:active { - box-shadow: 0 0 2px 2px #eee inset; - border-bottom-width: 1px; -} +h1 + position absolute + top 1em + left 3em + font-weight 200 + font-size 1em + line-height 1em + padding 0 + margin 0 + text-transform uppercase + color #ccc + +.linenos + padding-right .5em + color #ccc + border-right #eee 1px solid + +.code + padding-left .5em + +.container + width 100% + height 100% + +textarea, input + outline none + +textarea + background-color #ffffff + width 100% + height 100% + line-height 120% + resize none + border #ddd 1px solid + border-radius 5px + padding .5em + +form input[type="submit"], form a[role="submit"] + float right + +form a[role="submit"] + background-color #fefefe + background-image -webkit-linear-gradient(top, #ffffff, #f5f5f5) + background-image linear-gradient(to bottom, #ffffff, #f5f5f5) + box-shadow 0 1px 2px 2px #ffffff + color #666 + text-shadow 0 0 2px #ffffff + border #ddd 1px solid + border-radius 4px + border-bottom-width 4px + font-size 1em + width 5em + text-align center + padding .4em 0 + margin .4em 0 + text-decoration none + display none + + &:active + box-shadow 0 0 2px 2px #eee inset + border-bottom-width 1px diff --git a/app/config.example.rb b/app/config.example.rb deleted file mode 100644 index 9b2c9e6..0000000 --- a/app/config.example.rb +++ /dev/null @@ -1,3 +0,0 @@ -CONFIG = { - database_url: 'postgres://pastemaster:pastemaster@localhost:5432/pastemaster' -} diff --git a/app/models/configuration.rb b/app/models/configuration.rb new file mode 100644 index 0000000..5048c53 --- /dev/null +++ b/app/models/configuration.rb @@ -0,0 +1,28 @@ +class Configuration + attr_reader :database_url, + :syntaxes_map, + :available_syntaxes + + def initialize + load_database_config + load_syntax_config + end + + def load_database_config + config = YAML.load_file(APP_ROOT.join('config', 'database.yml')) + @database_url = '%s://%s:%s@%s:%s/%s' % [ + config['protocol'], + config['username'], + config['password'], + config['host'], + config['port'], + config['database'] + ] + end + + def load_syntax_config + config = YAML.load_file(APP_ROOT.join('config', 'syntax.yml')) + @syntaxes_map = config + @available_syntaxes = config.keys + end +end diff --git a/app/paste.rb b/app/models/paste.rb similarity index 85% rename from app/paste.rb rename to app/models/paste.rb index 06f0108..e84e6f2 100644 --- a/app/paste.rb +++ b/app/models/paste.rb @@ -10,7 +10,7 @@ class Paste def initialize(contents, type = nil) @contents = contents - @type = type if available_types.include?(type) + @type = type if CONFIG.available_syntaxes.include?(type) end def save @@ -47,8 +47,4 @@ class Paste def html type ? highlighted : paragraph end - - def available_types - @available_types ||= Pygments::Lexer.all.map(&:aliases).flatten - end end diff --git a/config/database.yml b/config/database.yml new file mode 120000 index 0000000..f227519 --- /dev/null +++ b/config/database.yml @@ -0,0 +1 @@ +examples/database.yml \ No newline at end of file diff --git a/config/examples/database.yml b/config/examples/database.yml new file mode 100644 index 0000000..18ed089 --- /dev/null +++ b/config/examples/database.yml @@ -0,0 +1,6 @@ +protocol: postgres +username: pastemaster +password: pastemaster +host: localhost +port: 5432 +database: pastemaster diff --git a/config/examples/syntax.yml b/config/examples/syntax.yml new file mode 100644 index 0000000..17492c5 --- /dev/null +++ b/config/examples/syntax.yml @@ -0,0 +1,32 @@ +actionscript: ActionScript +applescript: AppleScript +awk: Awk +c: C +charp: C# +cpp: C++ +clojure: Clojure +coffeescript: CoffeeScript +cl: Common Lisp +d: D +dart: Dart +delphi: Delphi +erlang: Erlang +go: Go +groovy: Groovy +haskell: Haskell +java: Java +javascript: JavaScript +json: JSON +lua: Lua +objc: Objective-C +ocaml: OCaml +perl: Perl +php: PHP +python: Python +ruby: Ruby +rust: Rust +scala: Scala +smalltalk: Smalltalk +vim: VimL +xml: XML +xslt: XSLT diff --git a/config/initializers/configuration.rb b/config/initializers/configuration.rb new file mode 100644 index 0000000..59875ef --- /dev/null +++ b/config/initializers/configuration.rb @@ -0,0 +1,2 @@ +APP_ROOT = Pathname.new(File.expand_path('../../..', __FILE__)) +CONFIG = Configuration.new diff --git a/app/database.rb b/config/initializers/database.rb similarity index 72% rename from app/database.rb rename to config/initializers/database.rb index ec28011..78c41bf 100644 --- a/app/database.rb +++ b/config/initializers/database.rb @@ -1,9 +1,9 @@ -DB = Sequel.connect(CONFIG[:database_url]) +DB = Sequel.connect(CONFIG.database_url) # DB.drop_table(:pastes) if DB.table_exists?(:pastes) DB.create_table(:pastes) do primary_key :id - String :type + String :syntax Text :contents end unless DB.table_exists?(:pastes) diff --git a/config/syntax.yml b/config/syntax.yml new file mode 120000 index 0000000..1e31d3f --- /dev/null +++ b/config/syntax.yml @@ -0,0 +1 @@ +examples/syntax.yml \ No newline at end of file diff --git a/pastemaster.rb b/pastemaster.rb index f6d8379..d7ece28 100644 --- a/pastemaster.rb +++ b/pastemaster.rb @@ -2,6 +2,7 @@ $:.unshift File.dirname(__FILE__) require 'securerandom' require 'base64' +require 'yaml' require 'bundler/setup' require 'sinatra' @@ -13,11 +14,12 @@ require 'stylus' require 'stylus/tilt' require 'pygments' +require 'app/models/configuration' +require 'app/models/paste' +require 'config/initializers/configuration' +require 'config/initializers/database' require 'lib/error_pages' require 'lib/assets' -require 'app/config' -require 'app/database' -require 'app/paste' class Pastemaster < Sinatra::Application set :server, 'unicorn' @@ -30,14 +32,8 @@ class Pastemaster < Sinatra::Application use CoffeeAssets use StylusAssets - def fetch_lexers - @lexers_list ||= Pygments::Lexer.all.map do |lexer| - { alias: lexer.aliases.first, name: lexer.name } - end.sort_by{ |a| a[:name] } - end - get '/' do - @lexers = fetch_lexers + @lexers = CONFIG.syntaxes_map slim :form, layout: :default end diff --git a/views/default.slim b/views/default.slim index 10b87ff..f5fe7b8 100644 --- a/views/default.slim +++ b/views/default.slim @@ -7,9 +7,10 @@ html link rel="stylesheet" href="/assets/dropdown.css?r1" link rel="stylesheet" href="/pygments_solarized_modified.css?r1" body - h1 - strong Paste - | master + a href="/" + h1 + strong Paste + | master .container== yield script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js" script type="text/javascript" src="/assets/pastemaster.js?r1" diff --git a/views/form.slim b/views/form.slim index b74cbdb..8e59a0c 100644 --- a/views/form.slim +++ b/views/form.slim @@ -5,7 +5,7 @@ form action="/" method="post" accept-charset="UTF-8" span Syntax ul.dropdown li.selected data-value="" Plain Text - - for lexer in @lexers - li data-value=lexer[:alias] =lexer[:name] + - for code, name in @lexers + li data-value=code =name input type="submit" value="Save" a href="#" role="submit" Save