LMSouq
moodle-core Open

Using awk to process html-related Gift-format Moodle questions

KE
kebs
1 month ago
4 views
Problem Description
This is basically a awk question but it is about processing data for the [Moodle][1] [Gift][2] format, thus the tags. I want to format html code in a question (Moodle "test" activity) but I need to replace < and > with the corresponding entities, as these will be interpreted as "real" html, and not printed. However, I want to be able to type the question with regular code and post-process the file before importing it as gift into Moodle. I thought awk would be the perfect tool to do this. Say I have this (invalid as such) Moodle/gift question: ::q1::[html]This is a question about HTML: <pre> <p>some text</p> </pre> and some tag:<code><img></code> {T} What I want is a script that translates this into a valid gift question: ::q1::[html]This is a question about HTML: <pre> <p>some text</p> </pre> and some tag:<code><img></code> {T} key point: replace < and > with `<` and `>` when: 1. inside a `<pre>`-`</pre>` bloc (assuming those tags are alone on a line) 1. between `<code>`and `</code>`, with arbitrary string in between. For the first part, I'm fine. I have a shell script calling awk (gawk, actually). awk -f process_src2gift.awk $1.src >$1.gift with process_src2gift.awk: BEGIN { print "// THIS IS A GENERATED FILE !" } { if( $1=="<pre>" ) # opening a "code" block { code=1; print $0; } else { if( $1=="</pre>" ) # closing a "code" block { code=0; print $0; } else { # if "code block", replace < > by html entities if( code==1 ) { gsub(">","\\>"); gsub("<","\\<"); } print $0; } } } END { print "// END" } However, I'm stuck with the second requirement.. Questions: 1. Is it possible to add to my awk script code to process the hmtl code inside the `<code>` tags? Any idea ? I thought about using sed but I didn't see how to do that. 1. Maybe awk isn't the right tool for that ? I'm open for any suggestion on other (standard Linux) tool. [1]: https://en.wikipedia.org/wiki/Moodle [2]: https://docs.moodle.org/38/en/GIFT_format

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base