--- Log opened Sun Nov 28 00:00:08 2021 |
01:24 | | Vornicus [Vorn@ServerAdministrator.Nightstar.Net] has quit [Connection closed] |
01:29 | | SmithKurosaki [uid215460@Nightstar-e2nmdb.irccloud.com] has joined #code |
01:38 | <&ToxicFrog> | Ok, what the hell is going on here |
01:38 | <&ToxicFrog> | LS_COLORS on the laptop works fine |
01:38 | <&ToxicFrog> | LS_COLORS on the server, sshed in from the laptop, works fine |
01:38 | <&ToxicFrog> | LS_COLORS on the server, sshed in from the laptop and then attached to tmux, does NOT work; which is to say, ls appears to use the default colour scheme rather than the one I've configured. |
01:39 | <&ToxicFrog> | This is impossible to google for because for most people "LS_COLORS doesn't work in tmux" means "tmux is stripping the colour codes/displaying the wrong ones because I forgot to enable 256 colours", which is not the case here. |
02:15 | | Degi_ [Degi@Nightstar-jadjlm.pool.telefonica.de] has joined #code |
02:17 | | Degi [Degi@Nightstar-vsqk4l.pool.telefonica.de] has quit [Ping timeout: 121 seconds] |
02:17 | | Degi_ is now known as Degi |
03:47 | | McMartin [mcmartin@Nightstar-i80eaa.ca.comcast.net] has quit [[NS] Quit: Reboot for system upgrade] |
04:48 | | Vorntastic [uid293981@Nightstar-phvupn.irccloud.com] has joined #code |
04:48 | | mode/#code [+qo Vorntastic Vorntastic] by ChanServ |
04:54 | | McMartin [mcmartin@Nightstar-i80eaa.ca.comcast.net] has joined #code |
04:54 | | mode/#code [+ao McMartin McMartin] by ChanServ |
05:08 | | Kizor [a@Nightstar-nfsqa7.yok.fi] has quit [[NS] Quit: ] |
05:11 | | Kizor [a@Nightstar-nfsqa7.yok.fi] has joined #code |
06:07 | | catalyst [catalyst@Nightstar-ejd4sd.cable.virginm.net] has quit [The TLS connection was non-properly terminated.] |
06:17 | | McMartin [mcmartin@Nightstar-i80eaa.ca.comcast.net] has quit [[NS] Quit: brb reboot] |
06:35 | | McMartin [mcmartin@Nightstar-i80eaa.ca.comcast.net] has joined #code |
06:35 | | mode/#code [+ao McMartin McMartin] by ChanServ |
07:38 | | SmithKurosaki [uid215460@Nightstar-e2nmdb.irccloud.com] has quit [[NS] Quit: Connection closed for inactivity] |
09:50 | <@macdjord> | https://imgur.com/4gRzxQM |
10:58 | | catalyst [catalyst@Nightstar-ejd4sd.cable.virginm.net] has joined #code |
11:39 | | Emmy [Emmy@Nightstar-l49opt.fixed.kpn.net] has joined #code |
12:41 | | gnolam [lenin@Nightstar-j9ajs0.priv.bahnhof.se] has joined #code |
12:41 | | mode/#code [+o gnolam] by ChanServ |
15:09 | | Vornicus [Vorn@ServerAdministrator.Nightstar.Net] has joined #code |
15:09 | | mode/#code [+qo Vornicus Vornicus] by ChanServ |
17:07 | | Vorntastic [uid293981@Nightstar-phvupn.irccloud.com] has quit [[NS] Quit: Connection closed for inactivity] |
17:12 | | catalyst_ [catalyst@Nightstar-ejd4sd.cable.virginm.net] has joined #code |
17:12 | | catalyst [catalyst@Nightstar-ejd4sd.cable.virginm.net] has quit [Connection closed] |
18:15 | | catalyst [catalyst@Nightstar-0p3gif.dab.02.net] has joined #code |
18:16 | | catalyst_ [catalyst@Nightstar-ejd4sd.cable.virginm.net] has quit [Connection closed] |
18:17 | | catalyst_ [catalyst@Nightstar-ejd4sd.cable.virginm.net] has joined #code |
18:19 | | SmithKurosaki [uid215460@Nightstar-e2nmdb.irccloud.com] has joined #code |
18:19 | | catalyst [catalyst@Nightstar-0p3gif.dab.02.net] has quit [Ping timeout: 121 seconds] |
19:24 | <@celticminstrel> | XD |
20:25 | | catalyst_ is now known as catalyst |
21:18 | <@sshine> | I'm trying to create the world's simplest template system for HTML |
21:19 | <@sshine> | I've got a file, template.html, that contains a string %message_body%, and I'd like to copy this template into another .html file and substitute the template string with a generated HTML body. |
21:19 | <@sshine> | how do I do that using POSIX sh? |
21:19 | <@sshine> | I'm allowed to use basic UNIX tools, but I'd like to avoid regex if possible. |
21:20 | <@sshine> | I can see there is a 'replace' command that ships with MySQL, but I can't expect it to be available or easily downloadable on a standard Linux/FreeBSD. |
21:27 | <@celticminstrel> | I think I'd recommend grep? |
21:28 | <@celticminstrel> | If you do grep -F then it'll just find and replace a fixed string. |
21:28 | <@celticminstrel> | No regex involved. |
21:28 | <@celticminstrel> | Oh wait, I guess I'm getting confused with sed… it probably still has a way of doing that tho. |
21:29 | <@sshine> | hmm, I had a look at grep -F, and I think that's my best bet. |
21:30 | <@celticminstrel> | Well, it can probably still be done with grep -F, yeah. |
21:30 | <@celticminstrel> | Especially if that string is on a line of its own. |
21:31 | <@sshine> | I couldn't find an example of using grep -F to do search/replace without also using sed. |
21:32 | <@sshine> | with Perl I could do: perl -ple 's/%html_body%/\Q$ENV{"html_body"}\E/' |
21:32 | <@sshine> | with \Q...\E quoting the content of the environment variable, making it a verbatim substitution. |
21:34 | <@sshine> | with Ruby I could even do: ruby -p -e 'sub("%html_body",ENV["html_body"])', I think |
21:34 | <@sshine> | I'd just like something that works off a plain system because I'm going to run it in an arbitrary CI runner. |
21:35 | <@sshine> | Perl used to be ubiquitous, but I don't think it's too true any more. |
21:35 | <@celticminstrel> | I think you could do something like grep -F with -B100 to get the first part (but ignore the last line) and with -A100 to get the last part (but ignore the first line), but I'm not sure how to achieve the "ignore one line" part. |
21:35 | <@celticminstrel> | head and tail don't seem to support that, and nor does cut AFAICT. |
21:36 | <@celticminstrel> | Oh wait… doesn't sed support that…? |
21:39 | <@sshine> | yes, but doesn't sed s/// syntax expect the substitution to be a regex? |
21:39 | <@sshine> | my substituted text is an arbitrary piece of HTML, so it could contain a tutorial with sed code in it. |
21:42 | <@celticminstrel> | I figured it out. |
21:42 | <@celticminstrel> | sed -n '1!p' drops the first line of input, and sed -n '$!p' drops the last line |
21:43 | <@celticminstrel> | I wonder if the whole thing could be done by sed, instead of passing grep thru sed… the thing you're searching for is quite simple… |
21:46 | <@sshine> | incredibly simple. |
21:47 | <@sshine> | so simple I wonder why there isn't an obvious tool for it. |
21:47 | <@sshine> | MySQL ships with a command-line tool called 'replace' that does exactly this. |
21:48 | <@sshine> | http://fart-it.sourceforge.net/ |
21:48 | <@sshine> | :) |
21:50 | <@celticminstrel> | I think I've got it. |
21:51 | <@celticminstrel> | sed '/%message_body%/rfile_containing_actual_contents.html' seems to do exactly what you want. |
21:51 | <@celticminstrel> | Assuming %message_body% is on a line by itself. |
21:51 | <@celticminstrel> | It's still technically a regex, but there are no regex special characters in %message_body%. |
21:52 | <@celticminstrel> | Ah, you can put a space after the r, that makes it a bit clearer. |
21:53 | <@sshine> | ohhh! that's not bad. |
21:53 | <@celticminstrel> | Of course the file needs to be in the current working directory or be an absolute path. |
21:54 | <@celticminstrel> | …I wonder if it can use stdin… |
21:54 | <@celticminstrel> | No, probably not? |
21:54 | <@sshine> | that's exactly what I wondered. |
21:54 | <@celticminstrel> | Since stdin is the stream being edited, presumably… |
21:54 | <@celticminstrel> | Tho if it's not, then maybe? |
21:55 | <@celticminstrel> | Well, /dev/stdin works but - does not. |
21:56 | <@sshine> | each time I render a page, I've got two files, the template file and the rendered html body. which one goes into stdin doesn't matter much to me, but it sometimes matters to the program. so for sed, for example, it'd matter if the %html_body% variable is replaced on each stdin line. |
21:57 | <@celticminstrel> | If the rendered HTML body isn't already saved to disk, I guess it makes more sense for that one to be stdin. |
21:57 | <@celticminstrel> | So it becomes like, render_html | sed '/%message_body%/r /dev/stdin' |
21:59 | <@sshine> | well, then it'd be scanning for %message_body% in the rendered HTML, but not in the template. |
22:00 | <@celticminstrel> | Oh, I missed the second argument to sed there. |
22:00 | <@celticminstrel> | render_html | sed '/%message_body%/r /dev/stdin' template.html |
22:01 | <@sshine> | another quirk... I allow free names of files, and sed "/%html_body%/r $html_body_file" is vulnerable to injection of sed code in the filename. |
22:01 | <@sshine> | the thing with using power tools like regex, perl, sed, awk, is that they expect the replacement string isn't user input. |
22:01 | <@sshine> | I think I'm gonna go with Perl index/substr. kinda sucks. |
22:02 | <@celticminstrel> | Ahh… so this doesn't work, huh. That's too bad… |
22:02 | <@sshine> | yeah, I want something really dumb and I can't find it outside of making a Perl/Ruby/etc. script. |
22:02 | <@celticminstrel> | No wait. |
22:03 | <@celticminstrel> | I think that vulnerability can be avoided? |
22:03 | <@sshine> | by escaping? probably! but I'd much prefer to not even have to avoid it. :P |
22:03 | <@celticminstrel> | cat $html_body_file | sed '/%message_body%/r /dev/stdin' $template_file |
22:03 | <@sshine> | ohhh. |
22:04 | <@sshine> | doesn't /dev/stdin refer to $html_body_file? |
22:04 | <@celticminstrel> | It'll refer to the thing being piped into sed. |
22:04 | <@celticminstrel> | Which in this case, yes. |
22:05 | <@sshine> | wow, awesome. this is working perfectly. |
22:05 | <@sshine> | thanks a lot! |
22:15 | | PinkFreud [WhyNot@NetworkAdministrator.Nightstar.Net] has joined #code |
22:15 | | mode/#code [+o PinkFreud] by ChanServ |
22:36 | <@macdjord> | sshine: `cat -- "${html_body_file}" | sed '/%message_body%/r /dev/stdin' template.html` |
22:36 | <@macdjord> | ... CM beat me to it. |
22:38 | <@sshine> | apparently /r only reads, but doesn't delete %message_body%. how do I delete that line? '/%message_body%/d;r /dev/stdin' separates the r from the regex. |
22:39 | <@sshine> | pseudo-syntax: '/%message_body%/(d;r /dev/stdin)' |
22:39 | <@macdjord> | sshine: It should be `cat -- "${html_body_file}"`, not `cat $html_body_file`. Because $html_body_file might contain whitespace or look like an option. |
22:39 | <@sshine> | macdjord, I actually replaced the cat with '< "$html_body_file"' because shellcheck complained. |
22:40 | <@macdjord> | That works! |
22:40 | <@sshine> | also, it said "Useless cat." -- on behalf of all catgender, I took offense. https://lgbta.fandom.com/wiki/Catgender |
22:40 | <@macdjord> | sshine: I think `sed --expression='/%message_body%/d;/%message_body%/r /dev/stdin'` |
22:41 | | * sshine submits a PR. |
22:41 | <@sshine> | will that work, though? doesn't the second /%message_body%/ not trigger once the line is deleted? |
22:41 | <@sshine> | yep, won't work |
22:44 | <@macdjord> | Try `sed --expression='/%message_body%/d;/$/r /dev/stdin'` |
22:51 | <@sshine> | won't work |
22:51 | <@sshine> | sed -e '/%html_body%/ { d; r /dev/stdin }' "$template_file" < "$html_body_file" |
22:51 | <@sshine> | this is my latest attempt. it says sed: -e expression #1, char 0: unmatched `{' |
23:10 | | catalyst [catalyst@Nightstar-ejd4sd.cable.virginm.net] has quit [[NS] Quit: -a- IRC for Android 2.1.59] |
23:14 | | SmithKurosaki [uid215460@Nightstar-e2nmdb.irccloud.com] has quit [[NS] Quit: Connection closed for inactivity] |
23:15 | | Kindamoody [Kindamoody@Nightstar-eubaqc.tbcn.telia.com] has quit [Ping timeout: 121 seconds] |
23:23 | | catalyst [catalyst@Nightstar-ejd4sd.cable.virginm.net] has joined #code |
23:25 | | Emmy [Emmy@Nightstar-l49opt.fixed.kpn.net] has quit [Ping timeout: 121 seconds] |
23:37 | <&ToxicFrog> | sshine, macdjord: r needs to be terminated with a newline |
23:37 | <&ToxicFrog> | so something like: |
23:39 | <&ToxicFrog> | <"$body_file" sed -E -e '/%html_body%/ { |
23:39 | <&ToxicFrog> | d |
23:39 | <&ToxicFrog> | r /dev/stdin |
23:39 | <&ToxicFrog> | }' "$template_file" |
23:39 | <&ToxicFrog> | That said |
23:39 | <&ToxicFrog> | This has a land mine in that it will replace the ENTIRE LINE in which %html_body% appears |
23:39 | <&ToxicFrog> | So if it contains, say, '<body>%html_body%</body>', those <body> tags get deleted |
23:40 | <&ToxicFrog> | Doing this correctly in the general case in just sed is Hard. |
23:41 | <&ToxicFrog> | I would be inclined to just switch to awk or lua. |
23:44 | <&ToxicFrog> | (in lua it's something like: |
23:44 | <&ToxicFrog> | <"$body_file" lua -e 'html=io.open("template.html"):read("*a"); body=io.stdin:read("*a"); print((html:replace("%html_body%", function() return body end)))' |
23:44 | <&ToxicFrog> | ) |
--- Log closed Mon Nov 29 00:00:09 2021 |