How to include an image in a blog post using ReST ?

82 views
Skip to first unread message

S Labbé

unread,
Jul 29, 2011, 3:08:07 PM7/29/11
to blogofile-discuss
Hi,

In a .rst file, one can include an image by writting :

.. image:: picture.png

which will include the image in the local directory. But, the png will
be ignored if it is inside the _posts directory... What do you
suggest? Is there already a solution for this? Should I try to write a
filter that copy the picture into the good directory? Or should I put
the image inside an Image repository without underscore which wont' be
ignored?

Thank you,

Sébastien, Montréal

Claudinei Pereira

unread,
Jul 29, 2011, 4:41:09 PM7/29/11
to blogofil...@googlegroups.com
Hi,

I guess the _posts directory is intended to just archive the posts
sources. Images should be placed on an folder outside it. You can
create an "img" folder on your root blog folder and place your images
inside it, then they will be just copied to "_site/img" at build.
Then, you can reference the images on your posts using:

.. image:: /img/picture.png

I write my posts using markdown and that's the way i use images. I
believe it will work on rst also.

2011/7/29 S Labbé <sla...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "blogofile-discuss" group.
> To post to this group, send email to blogofil...@googlegroups.com.
> To unsubscribe from this group, send email to blogofile-disc...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/blogofile-discuss?hl=en.
>
>

--
www.claudineipereira.com

S Labbé

unread,
Jul 29, 2011, 6:28:16 PM7/29/11
to blogofile-discuss
Great, it works!

I haven't though about the slash / in front of img. It is really easy
like that.

Thank you!

On 29 juil, 16:41, Claudinei Pereira <cont...@claudineipereira.com>
wrote:

S Labbé

unread,
Aug 3, 2011, 9:04:27 AM8/3/11
to blogofile-discuss
Hi again,

I have another question about the same problem. Writing

.. image:: /img/picture.png

works well when site.url = "http://www.yoursite.com", but when I
change it to site.url = "http://www.yoursite.com/~ryan" then
picture.png is not found because it looks for http://www.yoursite.com/img/picture.png
instead of http://www.yoursite.com/~ryan/img/picture.png.

In a blog post, is there an easy way to include an image relative to
the site.url instead of relative to the root?

Thank you,

Sébastien

allan bailey

unread,
Aug 3, 2011, 1:07:54 PM8/3/11
to blogofil...@googlegroups.com
try

.. image:: image/picture.png


That way it's relative to the directory of the page referencing it.

-allan

> --
> You received this message because you are subscribed to the Google Groups "blogofile-discuss" group.
> To post to this group, send email to blogofil...@googlegroups.com.
> To unsubscribe from this group, send email to blogofile-disc...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/blogofile-discuss?hl=en.
>
>

--
Allan Bailey
zirpu...@gmail.com

Ryan McGuire

unread,
Aug 3, 2011, 1:13:27 PM8/3/11
to blogofile-discuss
I would be careful with relative links, you'll get it working on one
page and it will be broken on another. Consider the fact that
blogofile writes a given post in multiple places: a permalink page, in
category listings, in archives etc.

On Aug 3, 1:07 pm, allan bailey <zirpubo...@gmail.com> wrote:
> try
>
> .. image:: image/picture.png
>
> That way it's relative to the directory of the page referencing it.
>
> -allan
>
> On 3 August 2011 06:04, S Labbé <sla...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > Hi again,
>
> > I have another question about the same problem. Writing
>
> > .. image:: /img/picture.png
>
> > works well when site.url = "http://www.yoursite.com", but when I
> > change it to site.url = "http://www.yoursite.com/~ryan" then
> > picture.png is not found because it looks forhttp://www.yoursite.com/img/picture.png
> > instead ofhttp://www.yoursite.com/~ryan/img/picture.png.
>
> > In a blog post, is there an easy way to include an image relative to
> > the site.url instead of relative to the root?
>
> > Thank you,
>
> > Sébastien
>
> > --
> > You received this message because you are subscribed to the Google Groups "blogofile-discuss" group.
> > To post to this group, send email to blogofil...@googlegroups.com.
> > To unsubscribe from this group, send email to blogofile-disc...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/blogofile-discuss?hl=en.
>
> --
> Allan Bailey
> zirpubo...@gmail.com

Ryan McGuire

unread,
Aug 3, 2011, 1:15:03 PM8/3/11
to blogofile-discuss
So to be clear, I would do:

.. image:: /~ryan/img/picture.png

S Labbé

unread,
Aug 3, 2011, 1:28:33 PM8/3/11
to blogofile-discuss
Hi,

Thanks for the answers,

> So to be clear, I would do:
>
> .. image:: /~ryan/img/picture.png

In fact, my website will be hosted on two servers. For one of them, my
login is "~labbe" and for the other, it is "~labbes". So I would
prefer not to hardcode the complete path in the source files of the
blog posts...

So if there are no easy solution, then I am thinking of putting the
image in the same directory as the source file .rst of the blog post,
then write simply:

.. image:: picture.png

and write a filter which would read each lines starting with "..
image::" and copy that image to the permalink directory. This also
gives the advantage of having pictures related to a post to be in the
same directory. Before I start to code it, are you thinking of a
better solution?

Thank you again!

Sébastien

S Labbé

unread,
Aug 3, 2011, 1:35:54 PM8/3/11
to blogofile-discuss
> and write a filter which would read each lines starting with "..
> image::" and copy that image to the permalink directory.

Ok, I just realized that this filter solution would not work for the
same reason as Ryan McGuire mentioned above.

Sébastien

Ryan McGuire

unread,
Aug 3, 2011, 2:15:13 PM8/3/11
to blogofil...@googlegroups.com
You could just write a filter like this:

$$image("pic.png") 

Then the filter would simply return a path defined in your config someplace concatenated with the file. For instance:

def image(filename):
    return bf.config.image_dir + "/" + filename

You'd need to base your filter on the syntax highlighter to take advantage of the $$image("") type symantics.


Sébastien

S Labbé

unread,
Aug 3, 2011, 2:43:13 PM8/3/11
to blogofile-discuss
> You could just write a filter like this:
>
> $$image("pic.png")

Great. I will follow this advice.

> Then the filter would simply return a path defined in your config someplace
> concatenated with the file. For instance:
>
> def image(filename):
>     return bf.config.image_dir + "/" + filename

Now, a stupid question. How can I make the bf aware of the existence
of image_dir ? I tried writing

image_dir = 'blah'

inside the _config.py file, but when I import bf inside the filter, it
doesn't seem to know about my configuration. I tried

import blogofile as bf
import blogofile_bf as bf
from blogofile.cache import bf
print bf.config.image_dir

and always get

AttributeError: 'module' object has no attribute 'image_dir'

Also, trying to print bf.config.site.url inside the filter prints
"http://www.yoursite.com" instead of the site.url I really defined
inside my _config.py file.

Thanks again for your help,

Sébastien

S Labbé

unread,
Aug 3, 2011, 2:58:06 PM8/3/11
to blogofile-discuss
Hi again,

> Now, a stupid question. How can I make the bf aware of the existence
> of image_dir ?

I knew my question was stupid. The bf.config knows about image_dir
*inside* of the run function but not outside:

print bf.config.image_dir # raises an error
def run(content):
print bf.config.image_dir # is OK

Sébastien

Ryan McGuire

unread,
Aug 3, 2011, 3:18:07 PM8/3/11
to blogofile-discuss
> I knew my question was stupid. The bf.config knows about image_dir
> *inside* of the run function but not outside:
>
> print bf.config.image_dir   # raises an error
> def run(content):
>      print bf.config.image_dir    # is OK

This is a race condition, the file is imported before your _config.py
is ever loaded, so if it's outside a function the reference will be
undefined. All setup for a filter needs to be done in an init() or
run() method.
Reply all
Reply to author
Forward
0 new messages