Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

Die neue S Klasse

logo

Bash like string expansion in Objective-C

So lets dive right in with a real world example. mulle-readme-cms, uses the templating language interpreter mulle-scion to generate a README.md file from constituent files called “buds”.

In the README.md of mulle-core, the “bud” constituents.bud.md is responsible for providing the list of constituents, that look like this:

Constituent Description
mulle-allocator 🔄 Flexible C memory allocation scheme
mulle-buffer ↗️ A growable C char array and also a stream
mulle-time 🕕 Simple time types with arithmetic on timespec and timeval

Now the code that generates the table is this part of “constituents.bud.md”:


{% if [amalgamated count] %}
| Constituent                                  | Description
|----------------------------------------------|-----------------------
{%
   sorter = [NSSortDescriptor sortDescriptorWithKey:@"url" ascending:YES]
   for item in [amalgamated sortedArrayUsingDescriptors:[NSArray arrayWithObject:sorter]] %}
| [{{ [S $:"item.name#src/"] }}]({{ [S $:"item.url%@*"] }}) | {{ item.description }}
{% endfor %}
{% endif %}

The mulle-scion executable is linked against the MulleFoundation which has gained “Regular Expressions” and “Wildcards” support for NSString and now also links against MulleBashStringExpansion, which contains a class named “S”.

Inside the loop you can see the new “S Klasse” put to work. If you know your bashs string expansion, you will recognize how #src/ removes a prefix from a string, and %@* removes a suffix with wildcards.

Where does the data actually come from?

The value for amalgamated is taken from properties.plist, which in turn is generated by mulle-project-properties-plist, which the README CMS calls on demand.


Post a comment

All comments are held for moderation; basic HTML formatting accepted.

Name:
E-mail: (not published)
Website: