You are not logged in.

#1 2017-09-13 19:35:03

ohnonot
...again
Registered: 2015-09-29
Posts: 5,394
Website

Convert Markdown to BBcode on the Fly!

New repo: https://notabug.org/ohnonot/md2bb
Explanations in README.
_____________________________

For use in forums, like here.
typing out bbcode isn't fun. especially not on some non-english keyboard layouts.
javascript editors suck in different ways.
also, for some sites it's really better to leave javascript disabled.

solution:

  • type markdown

  • highlight text

  • press the hotkey associated to the below script

requirements:

Warning - do not use the method described below. Use the wrapper script which is now included in the repo!

explanation in post #11.


#!/bin/sh

win="$(xdotool getwindowfocus)"
log="$HOME/md2bb$(date '+%s').log"
tmpfile="$HOME/md2bb$(date '+%s').tmp"

exec 2>"$log"

trap "rm $tmpfile $log" EXIT

parser="$HOME/bin/md2bb.pl"

output="$(xclip -o | $parser > $tmpfile)"

while read line; do
	xdotool type --delay 0 --window "$win" "$line" 
	xdotool key --delay 0 --clearmodifiers --window "$win" KP_Enter 
done < "$tmpfile"

msg="$(<"$log")"

[[ "x$msg" != "x" ]] && notify-send "markdown 2 bbcode" "$msg"

exit 0

enjoy!

Last edited by ohnonot (2020-11-30 07:28:56)


Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos

Offline

#2 2017-09-14 00:14:11

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,858
Website

Re: Convert Markdown to BBcode on the Fly!

Thanks!

Actually, I haven't used markdown that much TBH.

ohnonot wrote:

typing out bbcode isn't fun
...
solution:

  • type markdown

So markdown is a lot easier to type than BBCode?


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#3 2017-09-14 00:43:21

vasa1
Member
Registered: 2015-09-29
Posts: 204

Re: Convert Markdown to BBcode on the Fly!

I've used markdown quite a bit. Geany has a plugin for displaying it as well. I too feel markdown is more convenient to type but I have trouble with "*" and "_". When a single "*" is used, a closing "*" is needed or all the text following the "*" is italicised.

So if I have something like this:

convert -delay 20 -loop 0 *.jpg fitts.gif

".jpg" and following text is italicised even if that was not my intention. A similar situation happens with "_" sometimes used in filenames.


Using the Openbox (3.5.2) session of Lubuntu 14.04 LTS but very interested in BL :)

Offline

#4 2017-09-14 05:24:44

ohnonot
...again
Registered: 2015-09-29
Posts: 5,394
Website

Re: Convert Markdown to BBcode on the Fly!

^ vasa1, something like that would have to go either in `code tags` or in a

    code block

. then the '*' doesn't get translated.

johnraff wrote:

So markdown is a lot easier to type than BBCode?

in my opinion, yes.
https://daringfireball.net/projects/markdown/basics
the thing is that i use it in other contexts as well - writing README's for my projects, blog entries etc. - as do many others.markdown follows a few simple rules, and the whole point of it is that it is readable both as is, and possible to make well-formatted html out of it.

if someone wants to try, please tell me if the pandoc part works well. i was having some unexplained problems at first, which then just went away... :scratch:

(and just now i noticed that pandoc took an awefully long time to fire up, but subsequent runs are near-instant)


Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos

Offline

#5 2017-09-14 20:07:13

ohnonot
...again
Registered: 2015-09-29
Posts: 5,394
Website

Re: Convert Markdown to BBcode on the Fly!

i'm still ironing out issues.

script updated in post #1.


Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos

Offline

#6 2017-09-15 05:13:54

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,267
Website

Re: Convert Markdown to BBcode on the Fly!

There certainly are gotchas with markdown (like many versions of it), but still my favorite.

https://brontosaurusrex.github.io/2017/ … n-for-you/

My entire blog/stretchbang is obviously markdown (with some html here and there). To play around: https://dillinger.io

Last edited by brontosaurusrex (2017-09-15 05:15:13)

Offline

#7 2017-09-16 09:37:09

ohnonot
...again
Registered: 2015-09-29
Posts: 5,394
Website

Re: Convert Markdown to BBcode on the Fly!

i realized that the original markdown is a fairly simple perl script.
it took me 1-2 hours to hack it to output bbcode instead!
https://notabug.org/ohnonot/md2bb.pl

i updated the first post.

the script itself is lightning fast,but xdotool seems to introduce some delay (even though --delay is set to 0)...

Last edited by ohnonot (2018-06-21 23:18:14)


Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos

Offline

#8 2017-09-18 00:57:23

misko_2083
Member
Registered: 2016-05-24
Posts: 434

Re: Convert Markdown to BBcode on the Fly!

Maybe if you run xdotool inside the perl script.
==> https://pastebin.com/0MpgpEcM <==

Could be a little faster because it's not creating or reading temp file.

#!/bin/sh

log="$HOME/md2bb$(date '+%s').log"

exec 2>"$log"

trap "rm $log" EXIT

parser="$HOME/bin/md2bb.pl"

xclip -o | $parser 2>"$log"

msg="$(<"$log")"

[[ "x$msg" != "x" ]] && notify-send "markdown 2 bbcode" "$msg"

exit 0

Што ни оштровиди ум сагледати не може - љубав превазилази.

Offline

#9 2017-09-19 17:22:35

ohnonot
...again
Registered: 2015-09-29
Posts: 5,394
Website

Re: Convert Markdown to BBcode on the Fly!

i replaced my md2bb.pl with yours, but i'm getting this error over and over:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `xdotool type --delay 0 --window 35651593 Caveat: I do not know (yet) how to incorporate changes made on the cloned repository, back into the source repository.  '

Fornow,IthinkIwillrewritesomescriptstomakeuseofthe[code]HOSTNAME[/code]environmentvariable:[code]pinebook[/code]vs[code]arch[/code].
You specified the wrong number of args.
Usage: type [--window windowid] [--delay milliseconds] <things to type>
--window <windowid>    - specify a window to send keys to
--delay <milliseconds> - delay between keystrokes
--clearmodifiers       - reset active modifiers (alt, etc) while typing
--args N  - how many arguments to expect in the exec command. This is
            useful for ending an exec and continuing with more xdotool
            commands
--terminator TERM - similar to --args, specifies a terminator that
                    marks the end of 'exec' arguments. This is useful
                    for continuing with more xdotool commands.
--file <filepath> - specify a file, the contents of which will be
                    be typed as if passed as an argument. The filepath
                    may also be '-' to read from stdin.
-h, --help             - show this help output

doesn't matter whether i use your wrapper script or do it manually with

xclip -o|md2bb.pl

Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos

Offline

#10 2017-09-19 20:50:30

misko_2083
Member
Registered: 2016-05-24
Posts: 434

Re: Convert Markdown to BBcode on the Fly!

ohnonot wrote:

i replaced my md2bb.pl with yours, but i'm getting this error over and over:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `xdotool type --delay 0 --window 35651593 Caveat: I do not know (yet) how to incorporate changes made on the cloned repository, back into the source repository.  '

Ah, special characters...
Need to escape them before running xdotool.  $line =~s/(\W)/\\$1/g;
This is the part in the script that works.

#### Process incoming text: ###########################
my $text;
{
	local $/;               # Slurp the whole file
	$text = <>;
}

my @lines = split /^/, (Markdown($text));
foreach my $line (@lines) {
	my $win = qx#xdotool getwindowfocus#;
	chomp $win;
	chomp $line;

	my $break = join(' ', 'xdotool', 'windowactivate', '--sync', $win, 'key', 'Return');
	if ($line eq "\n") {
		system $break;
	}
	elsif (substr($line, 0, 2) eq "--") {
		$line =~s/(\W)/\\$1/g;
		my $line = join('', '[code]', $line, '[/code]');
		my $cmd = join(' ', 'xdotool', 'type', '--delay', '0', '--window', $win, $line, "\n" );
		system $cmd;
	}
	else {
		$line =~s/(\W)/\\$1/g;
		my $cmd = join(' ', 'xdotool', 'type', '--delay', '0', '--window', $win, $line, "\n" );
		system $cmd;
	}
		system $break;
}
##############################################################################

Full ==>
https://pastebin.com/VNbixGR6


Што ни оштровиди ум сагледати не може - љубав превазилази.

Offline

#11 2017-09-21 06:06:54

ohnonot
...again
Registered: 2015-09-29
Posts: 5,394
Website

Re: Convert Markdown to BBcode on the Fly!

there was a big mistake in my wrapper script.
i bound it to Windows+B, but even though xdotool uses --clearmodifiers, this does not apply to the typed output as long as the windows key is still held down, and i was getting a lot of Terminals (Windows+T) and editor windows (Windows+E).
I'm not sure why that didn't happen to me straight from the beginning.

anyhow, i re-evaluated my xdotool approach and realized I'm doing this all wrong.
Instead of typing the output with xdotool, I should instead copy it back into the clipboard (with xclip again) and use xdotool to just type out 'Ctrl-V' once.

Voilá, much safer and quicker now.

I added the wrapper script to the repo and edited post #1, warning you to not use the methods described in this thread!

PS:
this means i also discarded misko_2083's approach. sorry.

Last edited by ohnonot (2017-09-21 06:10:21)


Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos

Offline

#12 2020-11-30 07:28:13

ohnonot
...again
Registered: 2015-09-29
Posts: 5,394
Website

Re: Convert Markdown to BBcode on the Fly!

I am still using this every day.
the repo has changed, it's now https://notabug.org/ohnonot/md2bb and uses existing tools (markdown sed perl) instead of a badly hacked version of the markdown script.

ohnonot wrote:

For use in forums, like here.
typing out bbcode isn't fun. especially not on some non-english keyboard layouts.
javascript editors suck in different ways.
also, for some sites it's really better to leave javascript disabled.

solution:

  • type markdown

  • highlight text

  • press the hotkey associated to the below script

requirements:


Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos

Offline

Board footer

Powered by FluxBB