Code Tiddler and Transclude few lines

248 views
Skip to first unread message

Atronoush

unread,
Nov 25, 2020, 4:35:46 PM11/25/20
to TiddlyWiki
I use a Tiddlywiki for my Python codes. So I have a tiddler like below

Title: Slab Catalyst
Body:
 # Problem data and parameters
    L= 1e-3        # pellet thickness, m
    Cao=0.2        # concen at the surface, kg-mol/m3
    k=1e-3         # reaction rate constant, 1/s
    Dab=1.2e-9     # diffusion coeff, m2/s
   
    # Calculate Thiele modulus
    phi=L * np.sqrt(k/Dab)
    
    def slab_exact(z):
        """ dcalculates the exact solution """
        x = 1 - z/L
        Ca=Cao*np.cosh(phi*x)/np.cosh(phi)
        return Ca

    def odefun(z,u):
        """ defines the set of odes """        
        u1, u2 = u
        dudz=[ u2,
               k/Dab*u1 ]
        return dudz
    
    def bcfun(ul, ur):
        """ defines the boundary conditions """
        residue=[ ul[0]-Cao,
                  ur[1] ]
        return residue

Then in another tiddler I explain how code works and I need to include few line of code and explain them. So for example in Tiddler B, I need to transclude like 3-5 and in another part transclude like 8-19

Using the Tiddlywiki filter operators it possible to process a tiddler contents and break into lines, I wish to have a macro able
transclude certain lines of code in another tiddler.

-Atro

Atronoush

unread,
Nov 25, 2020, 4:37:43 PM11/25/20
to TiddlyWiki
Correction:

Then in another tiddler I explain how code works and I need to include few lines of code and explain them. So for example in Tiddler B, I need to transclude lines 3-5 and in another part transclude lines 8-19  

Atronoush

unread,
Nov 25, 2020, 4:38:41 PM11/25/20
to TiddlyWiki
See also this discussion for possible solution

Eric Shulman

unread,
Nov 25, 2020, 5:26:14 PM11/25/20
to TiddlyWiki
Place the following in a tiddler (e.g., "ShowLinesMacro"), tagged with $:/tags/Macro"
\define showLines(tid,from,to)
<$vars length={{{ [[$to$]subtract[$from$]add[1]] }}}>
<$set name="lines" filter="[[$tid$]get[text]splitregexp[\n]first[$to$]last<length>]">
<pre><$list filter="[enlist:raw<lines>]" variable="line"><$text text=<<line>>/><br></$list></pre>
</$set>
</$vars>
\end

which can then be invoked using:
<<showLines "Slab Catalyst" 3 5>>
<hr>
<<showLines "Slab Catalyst" 8 19>>

enjoy,
-e

Atronoush

unread,
Nov 25, 2020, 5:50:10 PM11/25/20
to TiddlyWiki
Thank you Eric!

Works like a charm!

-Atro

TW Tones

unread,
Nov 25, 2020, 6:19:13 PM11/25/20
to TiddlyWiki
Eric/ Atronoush

Eric, I really like that. Just some thoughts as a result.
  • It would be nice to extend this also to a wikified version as well. 
  • And perhaps a code view with line numbers from the source.
  • Perhaps a tool for selecting the start and end lines in the source tiddler would be cool

Atronoush 
  • I imagine your code tiddler has a different "type" field set,  also you are using codemirror because its editor lists line numbers.
  • There are code highlighters available as well in tiddlywiki
Regards
Tones

Eric Shulman

unread,
Nov 25, 2020, 7:05:24 PM11/25/20
to TiddlyWiki
On Wednesday, November 25, 2020 at 3:19:13 PM UTC-8 TW Tones wrote:
  • And perhaps a code view with line numbers from the source.

Here's an updated version that add line numbers AND also fixes a bug whereby only one blank line was included in the output

\define showLines(tid,from,to)
<$vars length={{{ [[$to$]subtract[$from$]add[1]] }}}>
<$set name="lines" filter="[[$tid$]get[text]splitregexp[\n]addsuffix[ ]first[$to$]last<length>]">
<pre><ol start="$from$"><$list filter="[enlist:raw<lines>]" variable="line"><li><$text text=<<line>>/></li></$list></ol></pre>
</$set>
</$vars>
\end
 
Notes:
* The $set filter adds a space to end of each line.  This effectively preserves multiple blank lines by making them non-empty list items
* The output is wrapped in <ol start="$from$">...</ol> and uses <li>...</li>  instead of a trailing <br> to add the line numbers

enjoy,
-e

Eric Shulman

unread,
Nov 25, 2020, 7:21:02 PM11/25/20
to TiddlyWiki
On Wednesday, November 25, 2020 at 3:19:13 PM UTC-8 TW Tones wrote:
  • It would be nice to extend this also to a wikified version as well. 

Wikified output should be as simple as changing this line:
<pre><$list filter="[enlist:raw<lines>]" variable="line"><$text text=<<line>>/><br></$list></pre>  
to:
<$list filter="[enlist:raw<lines>]" variable="line"><<line>><br></$list>

(i.e., remove the <pre> wrapper and the use of the $text widget)

-e

TW Tones

unread,
Nov 25, 2020, 11:39:16 PM11/25/20
to TiddlyWiki
Eric,

Very impressive Thanks.

I am playing with further extension's. Love it.

Tones

Mohammad Rahmani

unread,
Jan 8, 2021, 2:40:31 AM1/8/21
to tiddl...@googlegroups.com
Hi Eric,

 I came to this thread and found it very interesting. One use case is to show a code using the $codeblock widget with language to highlight the code for those specified lines and show the rest without highlighting!

 I appreciate to instruct me how to do this. I thought to use the $codeblock widget instead of pre tag in showLines but this way I see fragmented output.
 

Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/f609beb5-695f-4b38-aff3-36afe498cde9n%40googlegroups.com.

Mohammad

unread,
Jan 8, 2021, 5:12:14 AM1/8/21
to TiddlyWiki
Hi,
I tried to implement a partial solution to transclude selected lines of code from a tiddler contains the whole code.
Then I transcluded the whole code with those selected lines. The whole code is opaque but those lines are clear with sharp contrast.
This way one can show a code with few lines stand out to get the viewer attention.

To give a try 
1. download attached example, 
2. drop on https://tiddlywiki.com/prerelease/   (use prerelease which has the Highlight.js installed)
3.  open test/code-lines  and see how the code looks like

You can do more examples by opening  test/code-lines  and editing the lines.

Note: I am not happy with this solution, it seems a little tedious and low in performance (a lot of <pre><code> tags are created. What is the better solution.
standout-lines-of-code.json

Mohammad Rahmani

unread,
Jan 8, 2021, 5:21:26 AM1/8/21
to tiddl...@googlegroups.com
To explain the solution I just posted
The modified code is as below. The issue here is if you remove the $codeblock and pass the whole macro output to a single $codeblock the macro content will be displayed! 
If you use the $wikify widget the line breaks will be removed. So what do you think?

\define showLinesc(tid,from,to,language)
<$vars length={{{ [[$to$]subtract[$from$]add[1]] }}}>
<$set name="lines" filter="[[$tid$]get[text]splitregexp[\n]addsuffix[ ]first[$to$]last<length>]">
<ol start="$from$"><$list filter="[enlist:raw<lines>]" variable="line"><li><$codeblock code=<<line>> language=<<__language__>>/></li></$list></ol>
</$set>
</$vars> 
\end
 

Mohammad Rahmani

unread,
Jan 8, 2021, 10:56:53 AM1/8/21
to tiddl...@googlegroups.com
Attached is an interesting solution to transclude codes from a tiddler. This is useful when you want to discuss part of the code.

1. The code has a macro and a stylesheet
2. It is possible to transclude any part of code (say from line n to line m)  
4. Macro accepts ALSO a composite range like "2-5, 7 9, 13-18" and stands out those lines no matter in what order you pass the selected range
5. The stand out lines have different background and high contrast while other parts of the code are a little faded out
6. To give a try 
6.1. Download attached JSON
6.2. Drag and drop to https://tiddlywiki.com/prerelease/
6.3. Open highlight-selected-lines/test01

I would like to get feedback and tell me how the efficiency can be improved. 

Best wishes
Mohammad

The macro and its sub macro is given below ( highlight-selected-lines/macro)
\define hslines(tid, from, to, rng, language)
<$wikify name="numbers" text=<<rawlist>> >
<$vars length={{{ [[$to$]subtract[$from$]add[1]] }}} >
<$set name="lines" filter="[[$tid$]get[text]splitregexp[\n]addsuffix[ ]first[$to$]last<length>]">
<ol start="$from$" class="code-list">
<$list filter="[range[$from$,$to$,1]]" variable=num>
<$list filter="[enlist<numbers>nsort[]match<num>then[code-selected]else[code-notselected]]" variable=selectedClass>
<$vars pos={{{ [<num>subtract[$from$]add[1]] }}} >
<span class=<<selectedClass>> >
<li><$codeblock code={{{ [enlist:raw<lines>nth<pos>]  }}} language=<<__language__>>/></li> 
</span>
</$vars>
</$list>
</$list>
</ol>
</$set>
</$vars>
</$wikify>
\end

\define rng2list() {{{[range[$(start)$,$(end)$]addsuffix[ ]]}}}

\define rawlist()
<$list filter="[<__rng__>split[,]split[ ]!is[blank]]" variable=num>
<$list filter="[<num>search:title[-]]" emptyMessage=<<num>> variable=vrng>
<$vars start= {{{ [<vrng>split[-]first[]] }}}
       end=   {{{ [<vrng>split[-]last[]] }}} >
             <<rng2list>>
</$vars>			 
</$list>
</$list>
\end
standout-lines-of-code.json

Mohammad Rahmani

unread,
Jan 8, 2021, 11:40:05 AM1/8/21
to tiddl...@googlegroups.com
This second version of hslines even accepts a text as source code no need to provide a tiddler (of course I may recommend to put your code inside tiddler)
This can be used to teach Tiddlywiki scripts ;-)

A minor bug is version 1 has been fixed. You can set the opacity to blur the other lines .


See below examples

497_msedge.png


Best wishes
Mohammad

highlight-selected-lines-v2.json

TW Tones

unread,
Jan 8, 2021, 5:55:02 PM1/8/21
to TiddlyWiki
Mohamad et al

Nice work, I could imagine range sliders, one for the start line, one for the number of lines, at least for setting the "code window" to be displayed, at least in an authorship mode..

Tones

Mohammad Rahmani

unread,
Jan 8, 2021, 11:29:57 PM1/8/21
to tiddl...@googlegroups.com
Hi Tones

Thank you for your feedback. Yes the idea is great to have an interactive ui.
I also like to hear about the performance and see how I can improve the macro hslines.


Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages