Examples Refactoring

46 views
Skip to first unread message

Felipe Doria

unread,
Sep 7, 2010, 11:27:21 PM9/7/10
to prawn-ruby
Let me introduce myself first.

I'm Felipe Doria and currently a student of Greg Brown's RMU (Ruby Mendicant University). Each student is required to propose an individual project to accomplish during the course and I chose (with some help from Greg) to refactor Prawn examples.

Two of my ideas to improve the current set of examples are to reduce the number of reference pdf files to one file per example topic (text, bounding boxes, graphics, and so on), and to have self-documenting examples so that the generated pdfs revealed the code used to generate them.

My first very rough attempt on self-documenting examples can be found here. The generated document with code borrowed directly from the rounded polygons example can be seen here (I only committed this to be able to show it easily, I promise I won't do this in the future =)

I will appreciate if you guys help me with suggestions and overall Prawn knowledge cause if you help me code before the class ends Greg might find I'm cheating.

--Felipe Doria

Brad Ediger

unread,
Sep 8, 2010, 12:41:48 AM9/8/10
to prawn...@googlegroups.com
On Tue, Sep 7, 2010 at 10:27 PM, Felipe Doria <felipe...@gmail.com> wrote:
> Let me introduce myself first.
>
> I'm Felipe Doria and currently a student of Greg Brown's RMU (Ruby Mendicant
> University). Each student is required to propose an individual project to
> accomplish during the course and I chose (with some help from Greg) to
> refactor Prawn examples.

Hi Felipe,
It's good to hear you are working on the self-documenting examples.
Those will be very helpful to aid in our documentation.

The example you posted looks like a great start. I'd work on cleaning
up the look of the code -- set it in a monospace font and turn spaces
in the input text into non-breaking spaces so line indentation is
properly preserved.

One major issue with your Snippet module: I believe
Proc#source_location is 1.9-only. Since we support Ruby 1.8.7 as well,
you'll have to find some other way to extract the source text.

Thanks and keep up the good work!
Brad

Felipe Doria

unread,
Sep 8, 2010, 3:38:28 AM9/8/10
to prawn-ruby
On 8 September 2010 01:41, Brad Ediger <br...@bradediger.com> wrote:

The example you posted looks like a great start. I'd work on cleaning
up the look of the code -- set it in a monospace font and turn spaces
in the input text into non-breaking spaces so line indentation is
properly preserved.

Done. I still want to improve here with syntax highlighting and a nice box.
 

One major issue with your Snippet module: I believe
Proc#source_location is 1.9-only. Since we support Ruby 1.8.7 as well,
you'll have to find some other way to extract the source text.


Probably there is some better way to get the original code back from a Proc other than reading the file and relying on indentation. For the time being we can get the source_location info from Proc#to_s.

Thanks for the comments Brad. Please keep them coming.

-- Felipe Doria

Felipe Doria

unread,
Sep 21, 2010, 1:59:59 AM9/21/10
to prawn-ruby
You will find the code quite updated here: http://github.com/aguids/prawn/tree/examples/new_examples/

Single examples can be ran on their own, and each examples folder has a file that groups all the examples of that folder. The extraction of comments and code from the single examples is still rather weak using regular expressions.

On a recent 1-1 talk with Greg he told me to ask the Prawn devs a couple of points:

1) What will I need to do before merging it upstream?

2) Should I use some well defined comments to ease the code parsing? like:

# BEGIN_EXAMPLE
do_stuff
# END_EXAMPLE

3) Any other input or comment you might have about it. (This project has almost been accepted on RMU so any help will be welcome from now on and won't be considered cheating =D)

-- Felipe Doria

Brad Ediger

unread,
Sep 23, 2010, 4:15:09 PM9/23/10
to prawn...@googlegroups.com
On Tue, Sep 21, 2010 at 12:59 AM, Felipe Doria <felipe...@gmail.com> wrote:
> You will find the code quite updated here:
> http://github.com/aguids/prawn/tree/examples/new_examples/
>
> Single examples can be ran on their own, and each examples folder has a file
> that groups all the examples of that folder. The extraction of comments and
> code from the single examples is still rather weak using regular
> expressions.

This stuff looks great. I really like the direction this is going.

One note on Ruby 1.9.2 compatibility: The current directory (.) has
been removed from $LOAD_PATH. So requiring a file relative to the
current directory won't work. This can bite you in weird ways -- one
of which is that the previous require statements:

require File.join(File.dirname(__FILE__), '..', 'example_helper.rb'))

should be changed to use File.expand_path:

require File.expand_path(File.join(File.dirname(__FILE__),
'..', 'example_helper.rb'))

in order to be able to run the examples via a relative path (ruby
new_examples/graphics/hexagon.rb).

Similarly, try running your examples from outside of that directory --
for example, in Prawn's root directory:

ruby new_examples/graphics/graphics.rb

and fix any LoadErrors that pop there.

As for the text introducing the examples: It would be nice if you
could flow the text naturally instead of forcing linebreaks where they
appear in the source file. For example, in origin.rb, we probably
don't need a linebreak before "page".

> 2) Should I use some well defined comments to ease the code parsing? like:
>
> # BEGIN_EXAMPLE
> do_stuff
> # END_EXAMPLE

I think that this would probably be a good thing to do. I would prefer
if we could find a way around it, but I can't immediately think of a
way to avoid it if we're trying to pull the exact source text.

Eventually I can see something like this becoming Prawn's reference
manual. You could add in higher-level functions to create a chapter
for each directory (graphics being one). I'm sure you have thought of
these ideas already... I think this is off to a great start and I'm
excited to see where this can go.

Brad

Felipe Doria

unread,
Oct 9, 2010, 8:45:08 AM10/9/10
to prawn-ruby
I updated the inner workings of the example generator and finished the Graphics examples (file is attached).

Please reply with your feedback. Any help is welcome be it in regard to the code or to the document formatting.


On 23 September 2010 17:15, Brad Ediger <br...@bradediger.com> wrote:

Eventually I can see something like this becoming Prawn's reference
manual. You could add in higher-level functions to create a chapter
for each directory (graphics being one). I'm sure you have thought of
these ideas already... I think this is off to a great start and I'm
excited to see where this can go.


I'll start working on this direction. Is "Prawn by example" a good name?

-- Felipe Doria
graphics.pdf

Tom Dyer

unread,
Oct 9, 2010, 12:00:12 PM10/9/10
to prawn...@googlegroups.com
Wow, thank you, thank you. Very well done!

--
Tom Dyer

Gregory Brown

unread,
Oct 9, 2010, 12:14:30 PM10/9/10
to prawn...@googlegroups.com
On 10/9/10 8:45 AM, Felipe Doria wrote:
> I updated the inner workings of the example generator and finished the
> Graphics examples (file is attached).

Thank you so much Felipe. This looks excellent.

You've really set a high bar for what sort of great things can be
started through RMU projects.

-greg

Brad Ediger

unread,
Oct 9, 2010, 2:15:35 PM10/9/10
to prawn...@googlegroups.com
On Sat, Oct 9, 2010 at 7:45 AM, Felipe Doria <felipe...@gmail.com> wrote:
> I updated the inner workings of the example generator and finished the
> Graphics examples (file is attached).
>
> Please reply with your feedback. Any help is welcome be it in regard to the
> code or to the document formatting.

This is fantastic! As Greg said, you have truly set the bar high for
RMU projects, as well as for Prawn contributions. These examples are
incredibly clear and self-explanatory; excellent work.

I'll read over your code and post comments I may have, but I just
wanted to say that I'm very impressed with the result here.

-be

Felipe Doria

unread,
Oct 15, 2010, 12:46:40 PM10/15/10
to prawn-ruby
Guys,

I'm starting to lay the structure for the reference manual and would like some help on two points:

1. The current structure of running the example code inside the reference document pages might not work for some examples like those that depend on Prawn::Document.generate.
What should be done? Leave these examples off the manual? Show the code but don't execute it?

2. The first examples on the manual will be those about creating and generating Prawn::Document. After that what should be the order for examples or example packages?

--fd

Brad Ediger

unread,
Oct 19, 2010, 1:47:28 PM10/19/10
to prawn...@googlegroups.com
On Fri, Oct 15, 2010 at 11:46 AM, Felipe Doria <felipe...@gmail.com> wrote:
> Guys,
>
> I'm starting to lay the structure for the reference manual and would like
> some help on two points:
>
> 1. The current structure of running the example code inside the reference
> document pages might not work for some examples like those that depend on
> Prawn::Document.generate.
> What should be done? Leave these examples off the manual? Show the code but
> don't execute it?

I think it's fine if those examples aren't strictly self-executing.
But it would be good to have some information in the manual about how
Prawn::Document.generate behaves.

> 2. The first examples on the manual will be those about creating and
> generating Prawn::Document. After that what should be the order for examples
> or example packages?

Start with the simple stuff: basic graphics and text. Then you could
move on to page layout, more complex text formatting (like inline
formatting), tables, outlines, document security, and everything else.

-be

Felipe Doria

unread,
Nov 25, 2010, 11:37:22 PM11/25/10
to prawn-ruby
The first draft for the manual is attached.

It contains some explanation pages and four chapters: Basic Concepts, Graphics, Bounding Box and Text. I found that those are the chapters with the most basic functionality for beginners so they should come first.

If you think any important example or functionality regarding these first four chapters is missing or not covered please reply (Image will have its own chapter).

As usual please reply with your reviews.

I'll procede with the work on the other chapters.

--fd
manual.pdf

Tim Benest

unread,
Nov 26, 2010, 5:10:24 AM11/26/10
to prawn...@googlegroups.com
Excellent work, give yourself a pat on the back !

Tom Dyer

unread,
Nov 26, 2010, 12:06:39 PM11/26/10
to prawn...@googlegroups.com
Great stuff but I have a question about the below in the manual
"Reading the examples
The title of each example is the relative path from the Prawn source
examples/ folder."

I can't see the any of the examples in the examples dir? For example,
basic_concepts/creation.rb ?

thanks

--
Tom Dyer

Tom Dyer

unread,
Nov 26, 2010, 1:14:23 PM11/26/10
to prawn...@googlegroups.com
Ok, needed to clone https://github.com/aguids/prawn.git and switch to
the examples branch, got it.

--
Tom Dyer

Felipe Doria

unread,
Nov 26, 2010, 5:09:55 PM11/26/10
to prawn...@googlegroups.com
On 26 November 2010 16:14, Tom Dyer <tdy...@gmail.com> wrote:
Ok, needed to clone https://github.com/aguids/prawn.git and switch to
the examples branch, got it.
 
 
I wrote that as a placeholder as I see how it will be when the new examples get merged upstream. Right now they are here https://github.com/aguids/prawn/tree/examples/new_examples/
 
-- fd
Reply all
Reply to author
Forward
0 new messages