Skip to content

Commit

Permalink
Merge branch 'master' of github.com:galaxycats/ASS-Beginner
Browse files Browse the repository at this point in the history
* 'master' of github.com:galaxycats/ASS-Beginner:
  Generator now correctly escapes HTML with highlighting
  Path to 'ruby_essentials' was wrong
  Added more Ruby Code samples
  Added some console tweaking tipps
  Added basic style and syntax highlighting
  • Loading branch information
tandibar committed Apr 6, 2010
2 parents 6d356bb + 5b29dd1 commit 1367896
Show file tree
Hide file tree
Showing 13 changed files with 493 additions and 35 deletions.
31 changes: 28 additions & 3 deletions bin/md2site
Expand Up @@ -4,6 +4,10 @@ require 'rubygems'
require 'trollop'
require 'ftools'
require 'rdiscount'
require 'erb'
require 'uv'
require 'hpricot'
require 'cgi'

opts = Trollop::options do
version "md2site (c) 2010 Dirk Breuer"
Expand All @@ -18,8 +22,9 @@ EOS

opt :source_path, "Where to read the Markdown files from", :default => "site"
opt :destination_path, "Where to put the generated files", :default => "htdocs"
opt :config, "Path to config file with meta information", :default => "config.yml"
opt :nojekyll, "Put a .nojekyll file in destination_path", :default => true
opt :template, "Which template to use", :default => "templates/base.html.erb"
opt :theme, "Which syntax color scheme to use", :default => "sunburst"
end

class SiteGenerator
Expand Down Expand Up @@ -48,7 +53,7 @@ class SiteGenerator

def create_directory_structure
`rm -r #{@options[:destination_path]}`

@markdown_pages.each do |md_path, html_path|
File.makedirs("#{@options[:destination_path]}/#{File.dirname(html_path)}")
end
Expand All @@ -57,8 +62,28 @@ class SiteGenerator
def create_html_files
@markdown_pages.each do |md_path, html_path|
markdown = RDiscount.new(File.read(md_path))
File.open(File.join(@options[:destination_path], html_path), "w") { |f| f.write markdown.to_html }
File.open(File.join(@options[:destination_path], html_path), "w") { |f| f.write trough_template(markdown.to_html) }
end
Uv.copy_files "xhtml", @options[:destination_path]
end

def highlight_code_in_html(html)
html = Hpricot(html)
html.search('//pre/code') do |element|
element.inner_html =~ /^\!\!\!(\w+)/
element.inner_html = element.inner_html.gsub(/^\!\!\!(\w+)/, '')
element.inner_html = Uv.parse(element.to_plain_text, "xhtml", "#{$1 || 'ruby'}", false, @options[:theme])
end
html.to_html
end

def template
@renderer ||= ERB.new(File.read(@options[:template]))
end

def trough_template(content)
@content = content
highlight_code_in_html(template.result(binding))
end

def create_nojekyll
Expand Down
3 changes: 0 additions & 3 deletions config.yml

This file was deleted.

5 changes: 5 additions & 0 deletions site/handson/associations.md
Expand Up @@ -52,6 +52,7 @@ ableiten.
1:1-Beziehung (`has_one`) zwischen zwei Objekten: Ein `Picture` hat genau ein
`Thumbnail`.

!!!ruby_on_rails
class Picture < ActiveRecord::Base
has_one :thumbnail
end
Expand All @@ -69,6 +70,7 @@ ableiten.
1:m-Beziehung (`has_many`) zwischen zwei Objekten: Eine `Company` hat viele
`Client`s.

!!!ruby_on_rails
class Company < ActiveRecord::Base
has_many :clients
end
Expand All @@ -95,6 +97,7 @@ noch eine Klasse definiert werden.

Beispiel für `has_many_and_belongs_to`:

!!!ruby_on_rails
class Developer < ActiveRecord::Base
has_and_belongs_to_many :projects
end
Expand All @@ -114,6 +117,7 @@ Beispiel für `has_many_and_belongs_to`:

Beispiel für `has_many :through`:

!!!ruby_on_rails
class Developer < ActiveRecord::Base
has_many :assignments
has_many :projects, :through => :assignments
Expand Down Expand Up @@ -155,6 +159,7 @@ in den Controllern über die Methode `session` zugreifen. Das `session`-Objekt
ist dabei als Hash realisiert. Die Speicherung der ID eines Benutzers könnte
dementsprechend wie folgt aussehen:

!!!ruby_on_rails
user = User.find_by_username(params[:username])
if user
session[:user_id] = user.id
Expand Down
3 changes: 3 additions & 0 deletions site/handson/controller_und_views.md
Expand Up @@ -30,6 +30,7 @@ Die Aufgabe soll natürlich RESTful realisiert werden. Wir brauchen daher auf
jeden Fall drei Actions in unserem Controller. Hier noch einmal beispielhaft
die Implementierung der Action zu Anzeige aller Statusmitteilungen:

!!!ruby_on_rails
class MessagesController < ApplicationController

def index
Expand All @@ -43,6 +44,7 @@ Ordner `app/views/messages` verwenden. Im Template muss nicht mehr das HTML
Gerüst definiert werden, dass ist bereits über das Layout geschehen. Wir müssen also
nur noch alle Statusmitteilungen ausgeben. Dazu kann man sich folgenden Code vorstellen:

!!!html_rails
<% @messages.each do |message| %>
<% div_for message do %>
<p><%= message.content %></p>
Expand All @@ -69,6 +71,7 @@ verwendet. Für deren Realisierung zur Eingabe von Daten für ein
`ActiveRecord`-Objekt bietet Rails eine ganze Reihe von Helper-Methoden an. An
folgendem Beispiel seien einige dieser Methoden kurz beschrieben:

!!!html_rails
<% form_for :message do |f| %>
<%= f.text_area :content %>
<%= f.submit "Post Status" %>
Expand Down
1 change: 1 addition & 0 deletions site/handson/mentions_und_follower.md
Expand Up @@ -50,6 +50,7 @@ aufgerufen, nachdem eine bestimmte Methode ausgeführt wurde, wie z.B. die
`save`-Methode eines Active Record Objekts. Hier ein kleines Beispiel, um das
zu verdeutlichen:

!!!ruby_on_rails
class User < ActiveRecord::Base
after_save :send_welcome_email

Expand Down
2 changes: 2 additions & 0 deletions site/handson/partials.md
Expand Up @@ -26,6 +26,7 @@ Partials werden in Rails immer durch einen Underscore als Prefix im Dateinamen
identifiziert und werden in der gleichen Verzeichnisstruktur abgelegt wie die eigentlichen
Templates zu einem Controller:

!!!plain_text
app/
+-views/
+-messages/
Expand All @@ -36,6 +37,7 @@ Templates zu einem Controller:
Anhand des folgenden Beispiels wollen wir kurz erklären, wie Partials in Templates
integriert werden können:

!!!html_rails
<h1>Statusmitteilungen</h1>
<div>
<%= render "messages/form" %>
Expand Down
43 changes: 43 additions & 0 deletions site/handson/rails.md
Expand Up @@ -57,13 +57,15 @@ Datenbank-Tabellen angelegt werden.

### Rails installieren und Projektstruktur initialisieren

!!!plain_text
$> gem update --system
$> gem --version
1.3.6
$> gem install rails --pre

Rails-Projekt generieren:

!!!plain_text
$> rails <appname>

### Generatoren und rake-Tasks
Expand All @@ -73,6 +75,7 @@ Zunächst muss eine Resource mit Hilfe des folgenden Befehls generiert werden:
Dabei werden alle relevanten Dateien erzeugt, von der Migration über das
Modell bis hin zum Controller. Hier ein Beispiel:

!!!plain_text
rails generate resource message content:string
($> ~/projects/ass/twitter-clone)
invoke active_record
Expand Down Expand Up @@ -101,6 +104,7 @@ Jetzt kann man direkt die erzeugte Migration ausführen und den Server starten:
**Aber** wir wollen uns das ganze erstmal ohne Web Server auf der *Console*
ansehen:

!!!plain_text
$> rails console
irb> Message
=> Message(id: integer, content: string, created_at: datetime, updated_at: datetime)
Expand All @@ -110,6 +114,44 @@ ansehen:
=> true
irb> message
=> #<Message id: 1, content: "Dies ist meine erste Nachricht.", created_at: "2010-03-12 17:02:41", updated_at: "2010-03-12 17:02:41">
#### Einrichten der Rails Console

Die Console lässt sich um einige Gimmicks erweitern, wie etwa
Syntax-Highlighting. Eine weitere sinnvolle Anpassung ist die Umleitung der
Logausgaben auf STDOUT. So kann man direkt in der Console sehen wie etwa die
SQL-Query aussieht, die von Rails generiert wird. Um dass zu erreichen, müssen
zwei kleine Gems (Ruby Bibliotheken) installiert werden:

!!!plain_text
$> gem install wirble
$> gem install utility_belt

Danach muss noch eine `.irbrc`-Datei im Home-Verzeihnis angelegt mit folgendem
Inhalt angelegt werden:

# load libraries
require 'rubygems'
require 'wirble'
require 'utility_belt'

# start wirble (with color)
Wirble.init
Wirble.colorize

IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

if Object.const_defined?('Rails')
Rails.logger.instance_variable_set("@log", STDOUT)
end

Anschließend müssen nun noch die zuvor installierten Gems im Rails-Projekt
hinzugefügt werden. Dazu einfach folgende Zeilen im `Gemfile` des
Rails-Projektes hinzufügen:

gem "wirble"
gem "utility_belt"

### Konventionen

Expand All @@ -131,6 +173,7 @@ In der Routing-Datei (`config/routes.rb`) muss zu diesem Zeitpunkt nichts weiter
`root`-Route anzulegen. Diese beschreibt welche Action mit dazugehörigem Controller beim Aufruf der Haupt-URL (`http://localhost:3000`)
aufgerufen werden soll. Dazu fügt man folgenden Code am Ende der Routing-Definition ein:

!!!ruby_on_rails
route :to => "<controller>#<action>"

### Migrationen
Expand Down
4 changes: 3 additions & 1 deletion site/handson/ruby.md
Expand Up @@ -31,11 +31,13 @@ Program"](http://pine.fm/LearnToProgram/ "Learn to Program, by Chris Pine") von

* [Rails Searchable API Doc](http://railsapi.com/ "Rails Searchable API Doc")
* [Ruby Quick Reference Card](http://www.scribd.com/doc/7991776/Refcard-30-Essential-Ruby "Refcard #30: Essential Ruby")
* [Ruby Essentials](ruby_essentials.html "Ruby Essentials")

## Shortcuts

### Environment Setup

!!!plain_text
export PATH=/usr/local/bin:$PATH

### Strings
Expand Down Expand Up @@ -64,7 +66,7 @@ Program"](http://pine.fm/LearnToProgram/ "Learn to Program, by Chris Pine") von

4.times { |n| puts n }

while i &lt; 10
while i < 10
# do something
end

Expand Down

0 comments on commit 1367896

Please sign in to comment.