I said “sed”


Now it’s the turn for macros be fetched from the wiki page at http://www.freecadweb.org/wiki/index.php?title=Macros_recipes. We’ll need to parse the links (and macro names) from there. So we need some pattern to be able to parse similar data (i.e. links). The common thing was that the links had http://freecadweb.org ?title=Macro… So it was suggested by mentors that we can have a template with the links that can have a specific class.

So in the wiki source, the links are written as: [[Macro makeCube|Macro MakeCube]]. Here 1st argument (before the pipe) is the name of the page/URL. The second one is the link text that appears on the wiki. If we omit the second argument, then the first one is taken as the link text and URL.

So aim was to convert something like [[Macro makeCube|Macro MakeCube]] to {{MacroLink|Macro makeCube}}. Here MacroLink is the name of the template. Which is replaced by the tags we specify. Here it would be replaced by a span with class “MacroLink”.

Okay, so what’s the problem! There are more than 100 entries there and one would simply edit it one by one manually. So “sed” was there to rescue.

Now I had to look for some pattern to be able to replace text. So as we have to remove the content after the pipe (‘|’) and replace the [[]] with {{}} and add template tag and a pipe.

I am not good with regular expressions. On searching something similar, I found http://stackoverflow.com/a/10613688. So I got the idea and used it like:

sed ‘s/|Macro.*\]\]/\}\}/i’ input.txt > wikinew.txt

It selects the text starting from “|Macro” till “]]” and replace it by “}}” as we need it at the end. Hence, the content after pipe would be removed too. Now we’ll have entries like [[Macro makeCube}}.

sed -i.bak ‘s/\[\[Macro/\{\{MacroLink|Macro/’ wikinew.txt

Now it was the turn for the prefix. The above command will select text like [[Macro and will replace it by {{MacroLink|Macro. As we needed to add a template “MacroLink” to it. That fulfilled the need. Although, 2-3 entries needed to be modified manually. The final page is here (not sure if it would exist). Code

The code is https://github.com/mandeeps708/scripts/tree/master/FC-Wiki-template. I’m done!

Advertisements