\_@yarmand coding tales_/

Old school programmer adventures in a world of stratuper.

Embed Markdown Into Your HTML

| Comments

Sometime when you write documentation or HTML5 presentation (using slide.js for instance), you want to write a part of the document an easier way.

What about embeding directly some markdown code into you HTML and convert it on the flight on the browser when displaying ?

What I’d like to write

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<html>
  <body>
      <article>
         <script type="text/markdown">
The nice title here
=
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus diam sem.

then a little list
. item 1
. item2
        </script>
      </article>
  </body>
</html>

The on the flight conversion

showdown is the javascript markdown converter I will use.

At the end of my HTML file and before presentation runtime do its magick, I do the conversion on all markdown I can find in the file :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  <script src="http://www.showdown.im/showdown/example/showdown.js"> </script>
  <script>
    $(function() {
      // replace markdown text 
      $('script[type="text/markdown"]').after(function(index){
        converter = new Showdown.converter();
        return converter.makeHtml(this.textContent);
      });
    });
 </script>

Pour Une Vrai Popularité Représentée Par L’indice TIOBE

| Comments

Tiobe icon

Camile Roux nous indique sur RubyLive que Ruby conserve sa 11eme place, juste derrière Javascript au classement TIOBE

Le classement TIOBE utilise le mot popularité mais je pense que celui-ci est dévoyé dans ce cas. L’indice TIOBE mesure le nombre d’ingénieurs disponibles et ce nombre ne représente pas la joie que chacun éprouve dans tel ou tel langage. Il reflète simplement la demande du marché.

Je pense que de nombreux programmeurs ont un langage de prédilection pour leur activité professionnel mais cela ne reflète pas forcément leur goût.

Dans mon cas, je suis un gars plutôt pointu en C que je pratique depuis 20 ans, ce langage me fait manger mais je deteste. Le dimanche je fait du Ruby pour me reposer le cerveau et j’essaye de participer à la communauté pour rendre ce langage plus “business usable”.

Merci Camille de nous pointer vers cet indice qui nous montre que notre enthousiasme pour les langages moderne n’est pas refléter par le marché.

Aller les gars !!! Il faut pousser encore plus fort et convaincre les donneurs d’ordre pour que le TIOBE représente enfin la popularité du coeur des programmeurs.

Subject Following to Better Filter Social Network Posts

| Comments

who follow Twitter introduced public posting and follow concept from the beginning and have been copied by Google+ and more recently by Facebook with the subscribe button.

But profile follow is a one way selectors. It partially cover the issue because it ignore that a single person produce infos on differentes domains.

As a normal human, I am curious about several subjects and I produce infos about several subjects.

In my case, I produce informations as a software programer and as a dog trainer. These are completely unrelated subjects followed by completely different people I don’t want to bother with non accurate infos. To prevent that, I have to use multiple profiles and split my identity across them. It can become a nightmare or drive me Schizophrenic.

So following an identity is not enough. To be efficient we should follow somebody about subjects.

for example I can follow @DHHon Ruby or programming but not on skate boarding.

One social profile/identity should propose several subjects (including a global one). I should post to a subject the same way today I post to a google circle, but as the opposite of google circles, this is not me choosing people receiving infos, this is people choosing to follow my public subjects.

This way we get the double filter of choosing the most interesting people only on subject we care about.

The main drawback of this method is to reduce Serendipity which is the life of the internet. As a first response, I should say that every infos producer will have the responsibility of posting unrelated infos on subjects at a frequency acceptable to his followers.

Javascript Initializers to Your Views With Rails 3.1 and CoffeeScript

| Comments

The Problem

With the asset pipeline included in Rails 3.1, we have a great big javascript file including all at once our code.

But what about code isolation through all our sources files ?

CofeeScript respond to that perfectly with creation of one function object per file compiled. To add a global variable, you simply define it for the window object :

1
2
window.foo= () ->
  console.log('foo')

So all our javascript code is executed when it loads with good code isolation, but what if we want part of the code executed only for one view or all views of a controller ?

call initializers in your layout

I first wrote the gem Epyce to limit code loaded to only current controller and view, but this had 2 main issues :

  • loose benefit of one big js file to be put on a CDN
  • shared code between views must go to controller .js file

Then I rollback to the big application.js file and add initializers registration to my layout.

Like this for erb:

application.html.erb
1
2
3
4
5
6
    <script type='text/javascript'>
      $(document).ready( function() {
        Rails.init('<%="#{controller.controller_name}"%>');
        Rails.init('<%="#{controller.controller_name}_#{controller.action_name}"%>');
      });
    </script>

or if like me you are a haml:

application.html.haml
%script{type: 'text/javascript'}
  $(document).ready( function() { Rails.init("#{controller.controller_name}"); Rails.init("#{controller.controller_name}_#{controller.action_name}");});

Register initializers

Each initializer function can be binded to severals names.

By convention views are named => controllerName_viewName

here is an example of binding a initializer to the Person controller

persons.js.coffee
1
2
Rails.register_init 'persons', () -> 
  console.log('hello from Person controller')

Here is an example of binding several views to one initializer

persons.js.coffee
1
2
Rails.register_init ['persons_new','persons_edit'], () ->
  console.log('editing a Person')

initializers are cumulative

Calling register_init() several times on the same name cumulate initializers

for example:

1
2
3
4
5
Rails.register_init 'mytest', () -> console.log('foo')
Rails.register_init 'mytest', () -> console.log('bar')

# ... later in code ...
Rails.init('mytest')

will produce in console:

foo
bar

The registering code

To make all the previous code work, we need to define the Rails object.

application.js.coffee
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#= require_self
#= require_tree ./application

# From http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
fromPrototype = (prototype, object) ->
  newObject = Object.create(prototype)
  `for(prop in object)
    {
      if(object.hasOwnProperty(prop))
        newObject[prop] = object[prop];
    }`
  return newObject


window.Rails=fromPrototype Array,
  register_init: (names, fun) ->
    if(typeof(names.forEach) == 'undefined')
      n=names
      names=[n]
    for n in names
      previously_registered=this[n]
      this[n]=() ->
        if(typeof(previously_registered) != 'undefined')
          previously_registered.call()
        fun.call()
  init: (name) ->
    if(typeof(this[name]) != 'undefined')
      this[name]()

Note the usage of #= require_self to ensure your Rails object will be defined before including all our others files


One more thing

When registered, initializers can be called using init() and there name as string or directly on the Rails object.

1
2
3
4
5
6
Rails.register_init 'mytest', () -> console.log('foo')

# ... later in code ...
Rails.init('mytest')
# or directly
Rails.mytest()

Biblio and links

I will finish with a great thanks to Yehuda Katz for his 2 essential posts about Javascript :

links: