Skip to content

Instantly share code, notes, and snippets.

@huseyin
Last active December 20, 2017 18:09
Show Gist options
  • Save huseyin/57b0f9f8797ed59636e086ccfe8635be to your computer and use it in GitHub Desktop.
Save huseyin/57b0f9f8797ed59636e086ccfe8635be to your computer and use it in GitHub Desktop.
Wiki yönetim aracı
#!/usr/bin/env ruby
require 'rake/clean'
begin
require 'highline/import'
require 'command_line_reporter'
rescue LoadError
$stderr.puts(
'Aşağıdaki gemlerin kurulu olduğundan emin olun.', '',
'* highline',
'* command_line_reporter'
)
abort
end
include CommandLineReporter
HOMEFILE = 'Home.md'
TEMPFILE = 'Home-Temp.md'
# String =>
class String
def unindent
gsub /^[ ]+/, ''
end
end
def colorize(text, color_code)
"\033[1m\033[38;5;#{color_code}m#{text}\033[0m"
end
def red(text); colorize(text, 198); end
def green(text); colorize(text, 120); end
def blue(text); colorize(text, 117); end
def yellow(text); colorize(text, 190); end
def tabulation(hash)
table :border => true do
hash.each_pair do |tag, items|
row do
column(tag, align: 'center', color: 'blue')
items.each { |item| column(item, align: 'center', width: 25) }
end
end
end
end
def yesno(prompt)
while true
input = ask "#{prompt} [e/h]: "
return (input.first.downcase == 'e') ? true : false
end
end
def marklist(hash, opts = {})
contents = []
hash.each do |tag, sublist|
contents << "+ [#{tag}](#)\n"
unless sublist.empty?
sublist.each do |item|
title = item.gsub '-', ' '
file = opts[:local] == true ? item + '.md' : item
url = [tag, file].join '-'
contents << " - [#{title}](#{url})\n"
end
end
end
contents
end
CONJUNCTIONS = %w(
ve
veya
ama
fakat
yani
ancak
yada
dahi
)
def labelify(string)
string.gsub! /[_-]/, ' '
words = string.split ' '
if words.size == 1
labelify string.capitalize + '-' + 'Başlıksız Doküman'
else
words.map do |w|
CONJUNCTIONS.include?(w) ? w : w.capitalize
end.join '-'
end
end
FileItems = {}
Dir['*.md'].each do |mdfile|
parse = mdfile.split /-/
tag = parse.first
title = File.basename parse[1..-1].join('-'), '.md'
if FileItems.keys.include? tag
FileItems[tag] << title
else
FileItems[tag] = []
FileItems[tag] << title
end unless [HOMEFILE, TEMPFILE].include?(mdfile)
end
file TEMPFILE do
File.open(TEMPFILE, 'w') do |tf|
tf.write marklist(FileItems, :local => true)
end
end
CLEAN.include TEMPFILE
desc 'Yeni bir doküman oluştur'
task :new do
warn yellow(
<<-EOF.unindent
Wiki dokümanı oluştururken şu kriterlere dikkat edin:
1. Doküman ismi kuralı, '<tag> ...' şeklindedir.
2. İsimde kullanacağınız ilk kelime bir kategori teşkil eder.
3. Aynı kategoride kullanmak istediğiniz sayfaların ilk kelimelerini aynı tutun.
4. İsimlendirme sade ve anlamlı olsun.
5. Katı bir isimlendirme kuralı yoktur.
EOF
)
name = ask 'Doküman ismini giriniz: '
# İsimden etiketi al.
f = labelify(name) + '.md'
if yesno yellow("'#{f}' dokümanı oluşturulacak, izin veriyor musunuz?"), true
touch f
warn green("'#{f}' oluşturuldu")
else
exit 1
end
warn blue('Lütfen doküman derleme işlemini yapınız ( $ rake )')
end
desc 'Dokümanları listeye bas'
task :build => :clean do
File.open(HOMEFILE, 'w') do |f|
f.write marklist(FileItems)
end
end
desc 'Dokümanları lokal bir hostta test et'
task :view => TEMPFILE do
warn yellow(
<<-EOF.unindent
localhost çalıştırılacak!
Lütfen tarayıcınızda http://localhost:6419 adresine gidin
EOF
)
# İlgili komutu çalıştır. Hata varsa işlemi kes.
sh "grip #{TEMPFILE} >/dev/null 2>&1" do |ok|
unless ok
abort red(
<<-EOF.unindent
Bir şeyler ters gitti!
* 'grip' programı kurulu olmayabilir
* Arkaplanda bir proses daha çalışıyor olabilir
Bunlardan emin olun.
EOF
)
end
end
end
desc 'Dokümanları tablolu olarak göster'
task :table do
tabulation FileItems
end
task :default => [:build, :table]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment