How to get the relative URL for the rendered document? - docpad

Having set multiple urls the available relative*Path etc. from http://docpad.org/docs/meta-data return all the full path but not the rendered URL.
e.g. /en/some/path/to/my/document/title where the URL is /en/title
How do I get the rendered URL for a document?

Providing you are using eco, you should be able to do:
The primary url:
<%- #document.url %>
All the urls:
<%- JSON.stringify #document.urls %>

Related

Phoenix different title per page with root module

This question is similar to (but not an exact duplicate of) Phoenix Framework - page titles per route.
Ideally, i want to create titles like in the described question, but I am using a root layout since my project uses Phoenix LiveView. The HTML skeleton including the head and title HTML tag are part of the root template (root.html.eex). The app template extends on that from my understanding. I implemented the code from the above question
<title>
<%= if Kernel.function_exported?(#view_module, :title, 2) do %>
<%= #view_module.title(Phoenix.Controller.action_name(#conn), assigns) %> - StHub
<% else %>
StHub
<% end %>
</title>
and created a title function inside of my specific page view
defmodule StHubWeb.WowsView do
use StHubWeb, :view
def title(_action, _assigns) do
"Dashboard"
end
end
but the else branch of the code is triggered. Upon further inspection, I think that the issue is with using a root template, because the #view_module while rendering the root template is StHubWeb.LayoutView, and only inside of the LayoutView/app.html.eex template, the #view_module is my actual view (StHubWeb.WowsView).
I am not sure how to solve this other than removing the root template, but then my LiveView will have to contain the entire HTML skeleton all the time.
Maybe there is a way for me to define a title function in my LayoutView that will grab the title from StHubWeb.WowsView, but I am not sure how to do that.
Thanks for the help!

Convert actual image to base64 in Ruby

We're using a Rest endpoint that returns an actual image like so:
#image = RestClient.get('http://example.com/api/v1/scopes/12345/icon', headers).body
And the response is something like:
�PNG
���
IHDR���������H�۱���PLTE������� ��/��>��M��\��j��y������������������������������������������5��J��c�����������������3��i��������������:��Z��z�����������C��e��������������.��V��~���������(��U����������#��r�����K��|����������
��B��w���������+��f����*����E��������O������������T�������<����������F����������b��1�܀�����0����?�� �֟��N��S���������L�������������W��������m������s��������;��l��k��%�ڑ�����o��n����4��������`�����_�����؊���ْ�����$�����"�ڜ�������ց��d��������������!�ڥ��g�����������x��I�����և����X���ن�����H��,�ۿ����������p��)�����#��������D��}����h��8�ݝ��������2�ܳ��[������'��t��9��q��a������]��������6��������{�����������������
��u����-��7��P�����A��^����������������������R����=��Y��Q��&�ڼ�����G��v���بxi���=�IDATx���a�����ݳm�����3�L
We want to Base64 encode this and display it on a View in our Rails app:
<%= image_tag(Base64.encode(#image)) %>
However it seems that the Base64 is expecting us to Open a file and pass that... e.g.
<%= image_tag(Base64.encode(File.open(#image).read)) %>
But this API returns the actual image...
Tried using send_data as well...
Base64.encode64(send_data(RestClient.get('http://example.com/api/v1/scopes/12345/icon', headers).body, disposition: 'inline'))
But then just sends the image to the browser and bypasses our view...
How can we Base64 encode and display this image on our View?
Converting the binary data to base64 and passing that as data source isn't enough on its own. This would produce:
<img src="asdf...sdf==" />
Which isn't a valid source.
You have to create an data URI:
<%= image_tag("data:image/png;base64,#{Base64.encode64(#image)}") %>
This assumes your data has a MIME type of image/png. You could make this dynamic, but you have to extract the MIME type from the file or the extension somehow.
You might also be able to leave out the MIME type, since it's optional for data URIs. I'm not sure how the webbrowser would react to this though:
<%= image_tag("data:;base64,#{Base64.encode64(#image)}") %>
You could also create a helper for this. The asset pipeline already has a helper asset_data_uri. However since you fetch your images dynamically they are not part of the static server assets you might not be able to use it.

Middleman: choosing information from data files in frontmatter

I am using Middleman static page generator and I would like to pull information from data files based on selection made in frontmatter.
Example
I have data file located at data/cta.yaml with different variants of Call-To-Action text that can be repeated on various pages, meaning that each CTA text can be used on more than one page.
data/cta.yaml:
basic: This is default CTA
special: Something special here
other: Some other CTA
Then I have layout.erb:
<body>
<%= yield %>
<p class="cta">No data yet</p>
</body>
And test.html.erb:
---
title: Some page for testing
cta: It works with layout if I do not reference 'data/cta.yaml'
---
Some page content.
If I want to use, let's say, first CTA text, I could use <p class="cta"><%= data.cta.basic %></p> either in layout.erb layout file or remove it from layout and move it directly to the end of test.html.erb template file. Or, I could drop data file altogether and simply type CTA text for each page in frontmatter. However, I would prefer to keep CTA text in data file and all HTML in layout.erb and then be able to "choose" information from cta.yaml in test.html.erb frontmatter.
I tried to change
<p class="cta"><%= data.cta.basic %></p>
in layout.erb to
<p class="cta"><%= current_page.data.cta %></p>
and then in test.html.erb frontmatter:
---
title: Some page for testing
cta: data.cta.basic
---
but that resulted in verbatim data.cta.basic text instead of "This is default CTA" from cta.yaml data file.
Question
Is it possible at all to use frontmatter to select which text from data file should be used for given page?
As I mentioned in my comment, the frontmatter is parsed before the ERB, that’s the reason you are seeing data.cta.basic instead of the correct cta.
You could add a helper to achieve this though.
Here’s my helper
module CtaHelpers
def page_cta
cta = current_page.data.cta
data.cta.send(cta)
end
end
Here’s my test.html.erb file:
---
cta: special
---
<p class="cta"><%= page_cta %></p>
The test.html.erb file is calling the helper that determines from the Frontmatter which CTA to use, so the output is:
Something special here

Ruby - rails - how to create automatic hyperlinks for urls in the text/string rendered on the view?

How to create automatic hyperlinks for urls in the text/string rendered on the view?
I have a page that renders user activity log and in that log/text/string there are some random urls which I want to automatically hyperlink to open in a new browser window. There is this auto_link in ruby rails, how do I use that?
text = "User xyz accessed url - http://www.something.com on 04/13/2012 00:13:18GMT"
<%= "#{Text}" %>
I want this rendered with a hyperlink to the url. The URL could be anything anywhere in the text.
Use auto_link like this:
<%= auto_link(text) %>
If you want the generated links to open new browser windows (or tabs) add the option like so:
<%= auto_link(text, :html => { :target => '_blank' }) %>
As mentioned by pjumble in the comments, auto_link is no longer a part of Rails core as of Rails 3.1, this gem brings it back: https://github.com/tenderlove/rails_autolink Thanks pjumble!
For faster parsing, try https://github.com/vmg/rinku
require 'rinku'
Rinku.auto_link(text, mode=:all, link_attr=nil, skip_tags=nil)
Also one often need more filters than just linking, for example Youtube embedded videos. For this use: https://github.com/dejan/auto_html

how GetLocalResourceObject output an HTML string?

I stored some html contents in a local resource file.
So it has html tags in it such as p, br, div, etc.
I used GetLocalResourceObject("myContent")
to display the content on the page,
but the page doesnt render it as HTML.
It turned out that I overlooked the htmlhelper syntax.
I used <%= %> instead of <%: %>

Resources