Skip to content

Instantly share code, notes, and snippets.

@yannski
Last active March 1, 2017 14:42
Show Gist options
  • Select an option

  • Save yannski/b9bcd73af670d63bcddfebddbfc0b425 to your computer and use it in GitHub Desktop.

Select an option

Save yannski/b9bcd73af670d63bcddfebddbfc0b425 to your computer and use it in GitHub Desktop.
require "sinatra"
get '/' do
html = '<html><head><title>Mon blog</title><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"></head><body>'
html += '<div class="container"><div class="row"><div class="col-12">'
# récupération de la liste des noms de fichiers, triée par ordre alphabétique inverse
filenames = Dir.glob("articles/*.txt").sort.reverse
filenames.each{|filename|
# on lit le contenu du fichier
content = File.read filename
# on utilise String#lines pour récupérer toutes les lignes contenues dans le fichier
lines = content.lines
# on "décapsule" la première ligne avec Array#shift
first_line = lines.shift
# on copie le reste des lignes dans la variable other_lines
other_lines = lines
# la première ligne représente le titre
html << "<h1>"+first_line+"</h1>"
# les autres représentent le corps de texte. On joint les lignes avec Array#join
html << "<p>"+other_lines.join(" ")+"</p>"
}
html += "</div></div></div></body></html>"
return html
end
@Lightiix
Copy link
Copy Markdown

Lightiix commented Mar 1, 2017

Merci pour ce partage

@Derriick
Copy link
Copy Markdown

Derriick commented Mar 1, 2017

Merci pour ce partage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment