Is there already a script available that would turn selected text into a markdown table?
I could not find one, but I found a python script and tried to replicate it in AHK. I've spent over 11 hours on this, yesterday and today (3:45am now) so please don't think I didn't try and have now ditched my attempt. I am a bit out of my depth.
Please see the link below - it makes it clearer. The python script is what I was trying to replicate.
http://www.leancrew.com/all-this/2012/0 ... -textmate/
Thanks
Flory
Markdown table (example in python)
- SpeedMaster
- Posts: 494
- Joined: 12 Nov 2016, 16:09
Re: Markdown table (example in python)
Very interesting.
I wonder why a markdown table formatter hasn't been done yet in AHK. Anyhow...
St_Columnize() from the String Things library (by Tidbit) is the closest function you can get right now.
(Although the function does not manage the justification in the same way as a markdown table.)
String Things Library by Tidbit
https://autohotkey.com/boards/viewtopic.php?t=53
Cheers
I wonder why a markdown table formatter hasn't been done yet in AHK. Anyhow...
St_Columnize() from the String Things library (by Tidbit) is the closest function you can get right now.
(Although the function does not manage the justification in the same way as a markdown table.)
String Things Library by Tidbit
https://autohotkey.com/boards/viewtopic.php?t=53
Code: Select all
/*
To review briefly, the Normalize Markdown Table command takes a table that looks like this,
|Left align|Right align|Center align|
|:---------|----------:|:----------:|
|This|This|This|
|column|column|column|
|will|will|will|
|be|be|be|
|left|right|center|
|aligned|aligned|aligned|
and turns it into one that looks like this,
| Left align | Right align | Center align |
|:-----------|------------:|:------------:|
| This | This | This |
| column | column | column |
| will | will | will |
| be | be | be |
| left | right | center |
| aligned | aligned | aligned |
*/
text=
(
|Left align|Right align|Center align|
|:---------|----------:|:----------:|
|This|This|This|
|column|column|column|
|will|will|will|
|be|be|be|
|left|right|center|
|aligned|aligned|aligned|
)
gui, font, s18 cwhite, terminal
gui, color, black
gui, add, text, w600 h650, % " function st_columnize() by Tidbit`n`n" . st_columnize(text, "|", 2) . "`n`n" st_columnize(text, "|", 1) . "`n`n" st_columnize(text, "|", 3) . "`n`n`n`n How to handel a Markdown Table ? `n`n Thanks. "
gui, show, w650 h650
return
esc::exitapp
; String Things library by Tidbit
; https://autohotkey.com/boards/viewtopic.php?t=53
st_columnize(data, delim="csv", justify=1, pad=" ", colsep=" | ")
{
widths:=[]
dataArr:=[]
if (instr(justify, "|"))
colMode:=strsplit(justify, "|")
else
colMode:=justify
; make the arrays and get the total rows and columns
loop, parse, data, `n, `r
{
if (A_LoopField="")
continue
row:=a_index
if (delim="csv")
{
loop, parse, A_LoopField, csv
{
dataArr[row, a_index]:=A_LoopField
if (dataArr.maxindex()>maxr)
maxr:=dataArr.maxindex()
if (dataArr[a_index].maxindex()>maxc)
maxc:=dataArr[a_index].maxindex()
}
}
else
{
dataArr[a_index]:=strsplit(A_LoopField, delim)
if (dataArr.maxindex()>maxr)
maxr:=dataArr.maxindex()
if (dataArr[a_index].maxindex()>maxc)
maxc:=dataArr[a_index].maxindex()
}
}
; get the longest item in each column and store its length
loop, %maxc%
{
col:=a_index
loop, %maxr%
if (strLen(dataArr[a_index, col])>widths[col])
widths[col]:=strLen(dataArr[a_index, col])
}
; the main goodies.
loop, %maxr%
{
row:=a_index
loop, %maxc%
{
col:=a_index
stuff:=dataArr[row,col]
len:=strlen(stuff)
difference:=abs(strlen(stuff)-widths[col])
; generate a repeating string about the length of the longest item
; in the column.
loop, % ceil(widths[col]/((strlen(pad)<1) ? 1 : strlen(pad)))
padSymbol.=pad
if (isObject(colMode))
justify:=colMode[col]
; justify everything correctly.
; 3 = center, 2= right, 1=left.
if (strlen(stuff)<widths[col])
{
if (justify=3)
stuff:=SubStr(padSymbol, 1, floor(difference/2)) . stuff
. SubStr(padSymbol, 1, ceil(difference/2))
else
{
if (justify=2)
stuff:=SubStr(padSymbol, 1, difference) stuff
else ; left justify by default.
stuff:= stuff SubStr(padSymbol, 1, difference)
}
}
out.=stuff ((col!=maxc) ? colsep : "")
}
out.="`r`n"
}
stringTrimRight, out, out, 2 ; remove the last blank newline
return out
}
Re: Markdown table (example in python)
Thanks for that. I have already started on my script and it's all there apart from the string center function. It's nearly done, although there will be inefficiencies in my script as it's the first module I've written for ahk.
Please do not delete your post, I may need it for reference.
Cheers
Please do not delete your post, I may need it for reference.
Cheers
Who is online
Users browsing this forum: Lamron750 and 245 guests