Class: DrJekyll::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/drjekyll/catalog.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Catalog

Returns a new instance of Catalog.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/drjekyll/catalog.rb', line 24

def initialize( text )
  ## convert to "internal" inverted format (resolve shortcuts etc.)
  @themes = convert( YAML.load( text ) )

  ## auto-add keys for now (for quick testing)
  @themes.each do |k,h|
    name   = k
    keys   = h['keys'] || []
    if keys.empty?
      ## auto-add key from title/name
      ##   downcase and remove all non alphanumeric (non-ascii)
      slug = name.downcase.gsub( /[^a-z0-9]/, '' )

      ## for quick testing add some shortcuts
      if slug == 'planetjekyllsstarterminimal'
        keys << 'starter'
      elsif slug == 'drjekyllsminimal'
        keys << 'minimial'
      elsif slug == 'drjekyllsbootstrap'
        keys << 'bootstrap'
      elsif slug == 'drjekyllsclassicsbook'
        keys << 'classics'
      else
      end
      keys << slug
      h['keys'] = keys
    end
  end
end

Class Method Details

.from_file(path) ⇒ Object



13
14
15
16
17
# File 'lib/drjekyll/catalog.rb', line 13

def self.from_file( path )
  ## read in themes catalog
  text  = File.read( path )  ## fix: use File.read_utf8 ??
  self.from_string( text )
end

.from_string(text) ⇒ Object



19
20
21
# File 'lib/drjekyll/catalog.rb', line 19

def self.from_string( text )
  self.new( text )
end

.from_url(src) ⇒ Object



7
8
9
10
11
# File 'lib/drjekyll/catalog.rb', line 7

def self.from_url( src )
  worker = Fetcher::Worker.new
  text = worker.read_utf8!( src )
  self.from_string( text )
end

Instance Method Details

#convert(themes) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/drjekyll/catalog.rb', line 54

def convert( themes )
  ## see scripts in drjekyllthemes/build repo
  ##   todo/fix: use this class/code here in build script too - do NOT duplicate
  
  ## build hash with themes by title 
  hash = {}

  themes.each do |theme|
    title = theme.delete( 'title' )  ## remove title from hash and use as new key

    ## unify
    ##  check for github shortcut
    github = theme.delete( 'github' )
    if github
      theme[ 'home_url' ] = "https://github.com/#{github}"
      branch = theme.delete( 'branch' )
      branch = 'master'  if branch.nil?   ## if no branch listed assume master
      theme[ 'download_url' ] = "https://github.com/#{github}/archive/#{branch}.zip"
    end
    hash[ title ] = theme
  end
  hash
end

#find(key) ⇒ Object

method filter



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/drjekyll/catalog.rb', line 114

def find( key )   ## use fetch/get/? or different name - why? why not??
  ## find theme by key
  ##  fix/todo: use linear search for now; use hash lookup later

  theme = nil   ## note: return nil if nothing found

  @themes.each do |k,h|
    name   = k
    keys   = h['keys']

    if keys.include?( key )
      ## bingo found
      ##  todo: merge k (name/title) into (returned) hash - why? why not??
      puts " ** bingo; found theme >#{k}<:"
      pp h
      theme = h
      break
    end
  end

  theme
end

#list(filter = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/drjekyll/catalog.rb', line 79

def list( filter=nil )
  ## pp filter

  @themes.each_with_index do |(k,h),i|
    name   = k
    author = h['author']
    tags   = h['tags'] || []     ## allow nil for tags array for now
    keys   = h['keys']

    line = ''
    line << "  %3d" % (i+1)
    line << "..#{name}"
    line << " (#{keys.join(' | ')})"
    line << " by #{author}"
    if tags.nil? == false && tags.empty? == false
      line << " - "
      tags.each do |tag|
        line << "##{tag} "
      end
    end

    if filter
      ## note: ignore case (upper/lower/downcase) for now
      ##  filter on name/title and
      ##            tags for now
      if name.downcase.index(filter.downcase)             != nil ||    ## note: 0 is match found on index 0; only nil is not found
         tags.join('|').downcase.index( filter.downcase ) != nil
        puts line
      end
    else
      puts line
    end
  end
end