Redcarpet & Middleman: :with_toc_data - ruby-on-rails

I'm wondering about using Redcarpet's :with_toc_data option for Markdown working with Middleman (a Sinatra-based static site generator).
Our current config.rb:
set :markdown, :layout_engine => :haml
set :markdown_engine, :redcarpet
This doesn't work:
set :markdown, :layout_engine => :haml, :with_toc_data => true
set :markdown_engine, :redcarpet
Any help is very appreciated!

Fixed in Middleman 3.0 by Thomas Reynolds: https://github.com/middleman/middleman/issues/442

It appears that from Issue #200 of Middleman at Github, this should be done as such:
set :markdown, :layout_engine => :haml
set :markdown_engine, :redcarpet
set :redcarpet, :with_toc_data => true
The third line being the key. I also can't make this work, so there might be something still open as a bug with Middleman?
The latest release is 2.0.15.3, which is what I have installed; but I also can't make it work. Perhaps Issue #200 should be re-opened?
I have this exact code in my config.rb:
###
# GitHib flavoured Markdown, I can't go back!
###
set :markdown_engine, :redcarpet
set :redcarpet, fenced_code_blocks: true, autolink: true
I'd be eager to understand if I am doing something incorrectly. (I'm specifically trying to use this in a Middleman Blog)
Update to my answer: The commit referenced in Issue #200 does not exist in the 2.0.15.3 release, thus we'll have to use something newer.

Related

Rails: What is the correct usage of Rails.root.join when pointing to "tmp/caching-dev.txt"?

I'm setting up a new Rails project and after giving it an initial tidy with Rubocop, I'm left with a single offense.
Rubop complains:
config/environments/development.rb:16:6: C: Please use Rails.root.join('path', 'to') instead.
if Rails.root.join("tmp/caching-dev.txt").exist?
I see that Rails.root returns the path of the current project. So I've tried
if File.join(Rails.root, "tmp/caching-dev.text").exist?
instead. But still, Rubocop complains:
config/environments/development.rb:17:6: C: Please use Rails.root.join('path', 'to') instead.
if File.join(Rails.root, "tmp/caching-dev.text").exist?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What are the path and root arguments meant to be? Surely Rails.root is the path?!
I think Rubocop is suggesting you to do something like this
if File.exist?(Rails.root.join('tmp', 'caching-dev.txt'))
IMHO, Pathname and / are not used enough.
if (Rails.root / 'tmp' / 'caching-dev.txt').exist?
I solved problem by config .rubocop.yml:
Style/ExpandPathArguments:
EnforcedStyle: Style/ExpandPathArguments
Enabled: false

Dynamic CSS using Sprockets

I'm working on a site builder in rails and I would like to render the sites css using Sprockets SCSS processors. Since the user can change colors and logos, I can't use Sprockets precompilation so I've started working on a Rails SCSS template handler to handle dynamic views.
The goal is to compile 'app/views/sites/show.css.scss' any time /sites/43543.css is requested. Here's what I have so far. You'll notice I first run the template through the ERB processor and then attempt to run it through Sprockets.
https://gist.github.com/3870095
Manuel Meurer came up with an alternative solution that writes the ERB output to a path and then triggers the Asset Pipeline to compile it. I was able to get his solution to work locally but it wont work on heroku because the asset path is not writable. Files can only be written to the tmp directory and those files are only guaranteed for a single request.
http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-during-runtime/
After a long day I was able to solve my problem thanks to John Feminella and his post on google. The challenging part for me was figuring out how to create a new Sprockets::Context. Luckily John's solution doesn't require a Context.
Updated gist here
Attempt #1
This code does not allow importing css files from the asset pipeline.
#import "foundation"; works because foundation is loaded as a compass module
#import "custom_css"; results in an error message saying the file could not be found
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
options = Compass.configuration.to_sass_engine_options.merge(
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
)
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt #2
This code fails to embed base64 urls using asset-data-url
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt 3
Turns out you can use empty values while creating the context. Code below works in development but throws an error in production.
ActionView::Template::Error (can't modify immutable index)
It appears the error occurs in Sprockets::Index which is used instead of Sprockets::Environment in production. Switching to Sprockets::Environment doesn't solve the problem either.
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
context = CompassRails.context.new(::Rails.application.assets, '', Pathname.new(''))
resolver = ::Sass::Rails::Resolver.new(context)
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => resolver}
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end

rails 3, xml formatting and builder

I have an xml tag that needs to be formatted like so:
<AddDealRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
I can't seem to get this to work properly, using builder. I am attempting the following code in builder:
xml.AddDealRequest(:xmlns:xsi => "http://www.w3.org/2001/XMLSchema-instance", :xmlns:xsd => "http://www.w3.org/2001/XMLSchema" ) do
but obviously that second colon is throwing off the symbol. Is there any way to escape that second symbol? Or is this declaration entirely necessary?
Thanks!
Try quoting your symbols:
xml.AddDealRequest(
:'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
:'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)
You could also try using strings instead of symbols
xml.AddDealRequest(
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)
but I don't know if builder will be happy with that but the documentation includes things like this:
xm.target("name"=>"compile", "option"=>"fast")
# => <target option="fast" name="compile"\>
so strings for the attribute names should work.
A bit of time in irb might be help clarify things:
>> 'where_is:pancakes_house'.to_sym
=> :"where_is:pancakes_house"
>> :'xmlns:xsi'.to_s
=> "xmlns:xsi"
Rather than expect anyone to read through all the comments in the earliest answer, I'll just post the outcome here:
Firefox doesn't display the xmlns attribute (at least not when it matches a default). If you view the source (Ctrl+U) or use Chrome as your browser, you'll see that the missing attributes are appearing in the xml output.

How do I create a checkmark in unicode?

I would think it would be:
"✓".encode(:unicode)
But I think this is not the correct usage of .encode. And when I say:
"✓".encode('Unicode')
it cannot do the conversion.
If you're using Ruby 1.9 (which has much better built-in encoding support), you can do this:
> checkmark = "\u2713"
# => "✓"
> checkmark.encoding
# => #<Encoding:UTF-8>

FileTest.exists? issue with ruby on rails

I am trying to check if a file exists in my rails application.
I am running ruby 1.8.6 and rails 2.1.2 with windows XP.
So, the problem is that the FileTest.exists? method doesn't seem to be working. I have simplified the code to this point :
if FileTest.exists?("/images/header.jpg")
render :text => "yes"
else
render :text => "no <img src='/images/header.jpg' />"
end
If I do that the system displays "no" and then includes the image that displays correctly because /images/header.jpg exists.
I tried FileTest.exists?, FileTest.exist?, File.exists?, File.exist? and nothing seems to be working.
What am I doing wrong ?
Thanks
I'm guessing it's because you're asking whether a file "header.jpg" exists in a directory "images" off of the root directory for your system (which on Windows I'd assume is "c:\"). Try putting the full path (from the filesystem root) to the "/images" directory rather than the URL path.
In particular, as pointed out by #Brian, you should use:
FileTest.exists?(RAILS_ROOT + "/images/header.jpg") # < rails 3.0
FileTest.exists?(Rails.root + "/images/header.jpg") # >= rails 3.0
Add RAILS_ROOT to the filename that you're checking before calling exists?

Resources