Class: Siru::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/siru/cli.rb

Class Method Summary collapse

Class Method Details

.build(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/siru/cli.rb', line 70

def self.build(options = {})
  # Get the original working directory from environment variable or current directory
  original_dir = ENV['CD'] || Dir.pwd
  config_path = File.join(original_dir, 'config.toml')
  
  unless File.exist?(config_path)
    puts "Error: Not in a Siru site directory. Run 'siru new SITENAME' first."
    exit 1
  end
  
  Dir.chdir(original_dir) do
    config = Config.load
    site = Site.new(config, options)
    builder = Builder.new(site, options)
    builder.build
  end
end

.new_post(title, options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/siru/cli.rb', line 106

def self.new_post(title, options = {})
  # Get the original working directory from environment variable or current directory
  original_dir = ENV['CD'] || Dir.pwd
  config_path = File.join(original_dir, 'config.toml')
  
  unless File.exist?(config_path)
    puts "Error: Not in a Siru site directory. Run 'siru new SITENAME' first."
    exit 1
  end
  
  # Generate filename from title
  filename = title.downcase.gsub(/\s+/, '-').gsub(/[^a-z0-9\-]/, '') + '.md'
  filepath = File.join(original_dir, 'content', 'posts', filename)
  
  # Check if file already exists
  if File.exist?(filepath)
    puts "Error: Post '#{filepath}' already exists."
    exit 1
  end
  
  # Create the posts directory if it doesn't exist
  FileUtils.mkdir_p(File.dirname(filepath))
  
  # Generate post content
  date = Date.today.strftime('%Y-%m-%d')
  draft = options[:draft] ? 'true' : 'false'
  
  # Clean up the title (remove any surrounding quotes)
  clean_title = title.gsub(/^["']|["']$/, '')
  
  post_content = "    +++\n    title = \"\#{clean_title}\"\n    date = \"\#{date}\"\n    draft = \#{draft}\n    +++\n    \n    Write your post content here.\n  POST\n  \n  # Write the file\n  File.write(filepath, post_content)\n  puts \"Created new post: \#{filepath}\"\n  \n  # Open in editor\n  editor = ENV['EDITOR'] || ENV['VISUAL'] || 'nano'\n  system(\"\#{editor} \#{filepath}\")\nend\n"

.new_site(name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/siru/cli.rb', line 3

def self.new_site(name)
  puts "Creating new site: #{name}"
  
  # Get the original working directory from environment variable or current directory
  original_dir = ENV['CD'] || Dir.pwd
  target_dir = File.expand_path(name, original_dir)
  
  FileUtils.mkdir_p(target_dir)
  Dir.chdir(target_dir) do
    # Create directory structure
    %w[content content/posts static themes public].each do |dir|
      FileUtils.mkdir_p(dir)
    end
    
    # Create config file
    config = {
      'baseURL' => 'http://localhost:3000/',
      'languageCode' => 'en-us',
      'title' => name.capitalize,
      'theme' => 'paper',
      'params' => {
        'color' => 'linen',
        'bio' => 'A blog powered by Siru',
        'disableHLJS' => true,
        'disablePostNavigation' => true,
        'monoDarkIcon' => true,
        'math' => true,
        'localKatex' => false
      }
    }
    
    File.write('config.toml', TOML::Generator.new(config).body)
    
    # Create sample post
    sample_post = "      +++\n      title = \"Hello Siru\"\n      date = \"\#{Date.today.strftime('%Y-%m-%d')}\"\n      draft = false\n      +++\n      \n      Welcome to your new Siru site!\n      \n      This is your first post. Edit or delete it and start blogging!\n    POST\n    \n    File.write('content/posts/hello-siru.md', sample_post)\n    \n    # Copy theme files\n    siru_gem_dir = File.expand_path('../../..', __FILE__)\n    source_theme_dir = File.join(siru_gem_dir, 'themes', 'paper')\n    target_theme_dir = File.join('themes', 'paper')\n    \n    if Dir.exist?(source_theme_dir)\n      FileUtils.cp_r(source_theme_dir, 'themes/')\n      puts \"Theme 'paper' copied successfully\"\n    else\n      puts \"Warning: Theme 'paper' not found in \#{source_theme_dir}\"\n    end\n    \n    puts \"Site created successfully!\"\n    puts \"To get started:\"\n    puts \"  cd \#{name}\"\n    puts \"  siru serve\"\n  end\nend\n"

.serve(options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/siru/cli.rb', line 88

def self.serve(options = {})
  # Get the original working directory from environment variable or current directory
  original_dir = ENV['CD'] || Dir.pwd
  config_path = File.join(original_dir, 'config.toml')
  
  unless File.exist?(config_path)
    puts "Error: Not in a Siru site directory. Run 'siru new SITENAME' first."
    exit 1
  end
  
  Dir.chdir(original_dir) do
    config = Config.load
    site = Site.new(config, options)
    server = Server.new(site, options)
    server.start
  end
end