EDIT for clarity:
I'm wondering if it is possible to have set an output path for files in a Middleman build. For organizational purposes I want to group a type of page into a folder to keep it out of the main source directory. However on build/server I would like it to render to a different path:
/source
index.html
/landingpages
landingpage1.html
landingpage2.html
I have :directory_indexes enabled in my config file would like to be able to have the files in landingpage output to the root directory:
/build
index.html
/landingpage1
index.html
/landingpage2
index.html
Is this possible to achieve this somehow using the config.rb file and still show up properly in the sitemap? I would prefer to not have to do this using .htaccess
Thanks
A technique I used in a current project is based around proxies and should also solve your case:
landingpage_templates = Dir['source/landingpages/*.html']
landingpage_templates.map! do |tpl_name|
tpl_name = File.basename(tpl_name).gsub(/.html$/, '')
proxy "/#{tpl_name}/index.html", "/landingpages/#{tpl_name}.html", :ignore => true
end
You should be able to do something like that:
page "/file1/index.html", :proxy => "/somefolder/file1.html"
page "/file2/index.html", :proxy => "/somefolder/file2.html"
I think you're better off using directory indexes instead though and organising your files like:
/source
index.html
file1.html
file2.html
In your config.rb
activate :directory_indexes
Related
Lets suppose there is a web based documentation available at docs.project
Directory structure
/ - project root
/docs - documentation files in asciidoc format
index.adoc - documentation entry point
/public - public directory
generate.rb
Desirable workflow
I change documentation source in /docs directory.
I commit and push changes.
When pushing, the server runs ruby <project_root>/generate.rb command that rewrites the html presentation of the documentation.
index.adoc
= Documetation
Some text
[plantuml]
....
Client --> Server: Request
Server --> Client: Response
....
generate.rb
require 'asciidoctor'
require 'asciidoctor-diagram'
ROOT = File.dirname(__FILE__)
entry = ROOT + '/docs/index.adoc'
outdir = ROOT + '/public'
Asciidoctor.convert_file entry, to_dir: outdir, mkdirs: true
Problem
Current generate.rb script puts index.html file in public while images go to docs thus they are not available when you open docs.project in browser.
How to specify the images path?
You can use imagesdir
You could move all your images that are being used in your index.adoc into the public folder(since this is your output folder) and can set imagesdir document attribute like:
:imagesdir: ../public/
inside your index.adoc file. You could learn more about imagesdir here
Your problem should be that when using the Ruby API, the default safe mode is set to :secure, which should prevent access to files which reside outside of the parent directory of the source file. I am assuming to_dir is allowed to write the file, while plantuml goes through a different generation process (probably initially generates the image and then includes it, preventing access to the directory). In any case, you have two options.
Use unsafe mode with:
Asciidoctor.convert_file entry, to_dir: outdir, mkdirs: true, safe: :unsafe
It is probably not the best solution, use only if you are running it with controlled content (and no one can put something like include::/etc/passwd[] in the adoc source).
Change the base_dir with:
Asciidoctor.convert_file entry, base_dir: '.', to_dir: outdir, mkdirs: true
Which should work, assuming your directory structure looks like:
.
├── docs
│ └── index.adoc
├── generate.rb
└── public
I am trying to follow this tutorial:
http://net.tutsplus.com/tutorials/ruby/singing-with-sinatra/
Got stuck in "We’ll also make use of a “view file”, which allows us to split the markup for a view into a separate file. "
I have my basics.rb file running fine.
And My files are stored as follows:
Desktop/RubyForm/basics.rb
Desktop/RubyForm/view/form.erb
However, now when i go to http://localhost:9393/form , I am greeted with:
Errno::EIO at /form
Input/output error - <STDERR> file: lint.rb location: write line: 398
sinatra.error
Errno::ENOENT: No such file or directory -
/Users/HelenasMac/Desktop/views/form.erb
UPDATE! : Got the form to work right after running ruby basics.rb and going to http://localhost:4567/form .
However, after I run "shotgun basics.rb" , I have to go to
http://localhost:9393/form, and that's when the form doesn't show up.
What am I doing wrong? Disclaimer: mega beginner to ruby and using the terminal.
Thanks in advance!
If you cannot get shotgun to work then the new recommended way to reload Sinatra seems to be rerun.
To use it:
> gem install rerun
> cd /Users/HelenasMac/Desktop/RubyForm
> rerun ruby basics.rb
Explicity Set a Views Directory
Unless you're using inline template for your views with enable :inline_templates, you may need to explicitly define a template directory if the default values aren't working for you. The docs describe how to set your views directory as follows:
:views - view template directory
A string specifying the directory where view templates are located. By default, this is assumed to be a directory named “views” within the application’s root directory (see the :root setting). The best way to specify an alternative directory name within the root of the application is to use a deferred value that references the :root setting:
set :views, Proc.new { File.join(root, "templates") }
You may also need to explicitly set :root, and make sure that both :root and :views make sense from your current working directory.
I'm using the Middleman Blog gem for my site, but by default it appears the blog articles need to be located in /source which isn't particularly nice when looking at the tree in vim and trying to locate one of the other files in there (a template for instance).
From looking at the documentation I can't see if there is any way of moving the blog articles so they are stored somewhere else such as a blog_articles folder or similar.
Is this possible?
Put the following in your config.rb file.
activate :blog do |blog|
blog.permalink = ":year-:month-:day-:title.html"
blog.sources = "blog_articles/:title.html"
end
Assuming you have a post 2012-01-01-example-article.html.markdown stored in the folder source/blog_articles.
You should now see the post with this URL: http://localhost:4567/2012-01-01-example-article.html. (You might have to restart middleman when changing the config.rb file.)
Please note that I also had to set blog.permalink, the blog.sources setting alone didn't do the trick.
A bonus tip: I have activate :directory_indexes in my config.rb file. This setting gives you nice looking URLs, without the .html part.
If you want the same for your blog posts you can drop the .html from your blog.permalinksetting. Like so:
activate :blog do |blog|
blog.permalink = ":year-:month-:day-:title"
blog.sources = "blog_articles/:title.html"
end
Now you can see your post with this URL: http://localhost:4567/2012-01-01-example-article.
I messed with the middleman-blog extension, but gave up for its relative opaqueness. In looking at the source, though, it appears the prefix option might do the trick for you? It's somewhat unclear whether the prefix is a URL prefix or a local path prefix:
activate :blog do |blog|
blog.prefix = "/blog_articles"
end
From looking at the code it transpires there is a :sources option which you can use. If you poke around in the source there is an example of this:
https://github.com/middleman/middleman-blog/tree/master/fixtures/article-dirs-app
The solution above worked for me when I made the following changes to the permalink / source config options:
blog.permalink = ":title.html"
blog.sources = "posts/:year-:month-:day-:title.html"
The permalink is the location which it will appear in the web browser url where the source is the locations of the posts.
Using middleman 3.2.1
I made blog folder inside source directory. Then i make posts directory and moved all my posts there. source/blog/posts/...
and then inside config.rb
activate :blog do |blog|
..........
blog.permalink = "blog/:year/:month/:day/:title.html"
blog.sources = "blog/posts/:year-:month-:day-:title.html"
.........
end
I use Codeigniter framework , and you know when I try to load a config file then use it
I do something like that :
$this->load->config('myconfig', TRUE);
myconfig.php file is located inside application folder ( application/config/myconfig.php)
and use it like this :
$this->config->item('get_item', 'myconfig')
My question is : how can I change the location of myconfig file and use it properly ?
I want to put the config file(s) in out folder like this :
mysite -> system(folder)
mysite -> user_guide(folder)
mysite -> myConfigFiles(folder)
mysite -> myConfigFiles(folder) / myconfig.php
I need to do something like this :
$this->load->config(base_url.'myConfigFiles/myconfig', TRUE);
any help ?
Yes - it is possible to do this. The loader will accept ../../relative/paths. You can use a path relative from the default config directory (an absolute path will not work).
So let's say you have this structure (had a hard time following your description):
mysite
application
config <-- default directory
system
myConfigFiles
myconfig.php
You can just do this:
$this->load->config('../../myConfigFiles/myconfig', TRUE);
This works for pretty much everything - views, libraries, models, etc.
Note that with the introduction of the ENVIRONMENT constant in version 2.0.1, you can automatically check for config files within the config directory in another directory that matches the name of the current environment. This is really intended to be a convenience method for loading different files depending on if you are in production or development. I'm not 100% sure what your goals are, but this additional knowledge may also help you achieve them, or it may be totally irrelevant.
Really not sure WHY you would want to do this (and I wouldn't recommend it), but since all config files are is regular PHP files you can put a config file in the standard location that loads your extra config files. As an example:
mysite -> application -> config -> myconfigloader.php
then in myconfigloader.php put this:
<?php
require_once(APPPATH.'../myConfigFiles/myconfig.php');
So once you do
$this->load->config('myconfigloader', TRUE);
It will load everything in your myconfig.php file. Let me know if that works for you.
I have one page website only using HTML, CSS and JavaScript. I want to deploy the app to Heroku, but I cannot find a way to do it. I am now trying to make the app working with Sinatra.
.
|-- application.css
|-- application.js
|-- index.html
|-- jquery.js
`-- myapp.rb
And the following is the content of myapp.rb.
require 'rubygems'
require 'sinatra'
get "/" do
# What should I write here to point to the `index.html`
end
You can use the send_file helper to serve files.
require 'sinatra'
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
This will serve index.html from whatever directory has been configured as having your application's static files.
Without any additional configuration, Sinatra will serve assets in public. For the empty route, you'll want to render the index document.
require 'rubygems'
require 'sinatra'
get '/' do
File.read(File.join('public', 'index.html'))
end
Routes should return a String which become the HTTP response body. File.read opens a file, reads the file, closes the file and returns a String.
You could just host them from the public folder and they do not need routes.
.
-- myapp.rb
`-- public
|-- application.css
|-- application.js
|-- index.html
`-- jquery.js
In the myapp.rb
set :public_folder, 'public'
get "/" do
redirect '/index.html'
end
Link to some sub folder in public
set :public_folder, 'public'
get "/" do
redirect '/subfolder/index.html'
end
Everything in ./public is accessible from '/whatever/bla.html
Example :
./public/stylesheets/screen.css
Will be accessible via '/stylesheets/screen.css' no route required
Keep in mind that in production you can have your web server send out index.html automatically so that the request never gets to Sinatra. This is better for performance as you don't have to go through the Sinatra/Rack stack just to serve static text, which is what Apache/Nginx are awesome at doing.
Sinatra should let you serve static files from the public directory as explained in the docs:
Static Files
Static files are served from the ./public directory. You can specify a different location by setting the :public option:
Note that the public directory name is not included in the URL. A file ./public/css/style.css is made available as example.com/css/style.css.
Add below line in main rb file
set :public_folder, 'public'
ref: http://sinatrarb.com/configuration.html#static---enabledisable-static-file-routes
http://sinatrarb.com/configuration.html#static---enabledisable-static-file-routes
This would be the correct way of doing it.
set :public_folder, 'public'
I used the static setting because it's setting can affect the public_folder usage.
You can always use Rack::Static
https://www.rubydoc.info/gems/rack/Rack/Static
Just add this line before 'run' command into 'config.ru'
use Rack::Static, :urls => [""], :root => 'public', :index => 'index.html'
the sinatra-assetpack gem offers a whole bunch of features. syntax is sweet:
serve '/js', from: '/app/javascripts'
while i am still having issues with rails assets pipeline i feel like i have much more control using sinatra-assetpack - but most of the times it just works with a few lines of code.
UPDATED ANSWER:
I tied all the above with no luck of being ablle to load css, js....etc contents the only thing that was loading is index.html... and the rest were going =>> 404 error
My solution: app folder looks like this .
index.rb ==>> Sinatra code goes .
require 'rubygems'
require 'sinatra'
get '/' do
html :index
end
def html(view)
File.read(File.join('public', "#{view.to_s}.html"))
end
public folder==>> contains everything else ...css , js , blah blah..etc.
user#user-SVE1411EGXB:~/sintra1$ ls
index.rb public
user#user-SVE1411EGXB:~/sintra1$ find public/
public/
public/index.html
public/about_us.html
public/contact.html
public/fonts
public/fonts/fontawesome-webfont.svg
public/fonts/fontawesome-webfont.ttf
public/img
public/img/drink_ZIDO.jpg
public/js
public/js/bootstrap.min.js
public/js/jquery.min.js
public/js/bootstrap.js
public/carsoul2.html
public/css
public/css/font-awesome-ie7.css
public/css/bootstrap.min.css
public/css/font-awesome.min.css
public/css/bootstrap.css
public/css/font-awesome.css
public/css/style.css
user#user-SVE1411EGXB:~/sintra1$
Now start server and you will be able to navigate through static pages with no problem.
user#user-SVE1411EGXB:~/sintra1$ ruby index.rb
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop
require 'rubygems'
require 'sinatra'
set :public_folder, File.dirname(__FILE__) + '/../client'
#client - it's folder with all your file, including myapp.rb
get "/" do
File.read('index.html')
end
You might consider moving the index.html file to views/index.erb, and defining an endpoint like:
get '/' do
erb :index
end
Putting files in public folder has a limitation. Actually, when you are in the root '/' path is works correctly because the browser will set the relative path of your css file for example /css/style.css and sinatra will look for the file in the public directory. However, if your location is for example /user/create, then the web browser will look for your css file in /user/create/css/style.css and will the fail.
As a workaround, I added the following redirection to correctly load css file:
get %r{.*/css/style.css} do
redirect('css/style.css')
end
What about this solution? :
get "/subdirectory/:file" do
file = params[:file] + "index.html"
if File.exists?(params[:file])
return File.open("subdirectory/" + file)
else
return "error"
end
end
so if you now navigate to (for example) /subdirectory/test/ it will load subdirectory/test/index.html