Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Filename above the Indented code

first-time

Started by Zden2k 6 years ago · 6 replies · 565 views
6 years ago

Hello,
is there a plugin in Grav, that can display a label (for example file name) above the code on the page?

Like this:
code|411x181

Thanks.
Z.

6 years ago

Really no one knows how to do it? It does not have to be exactly what I placed here, would be enough if it looks like this:
code2|690x153

6 years ago

I'm not sure to understand what you're looking for.
Could you maybe be more specific and explain with more details ?

6 years ago

Thank you, I'm looking for a way to display the code in "Grav" with description.

I can insert the code, as follows:

TXT
#!/usr/bin/env python
#some code

but this show only code, I would like find a way for showing code with description for it. Just as in the picture.

last edited 02/28/20 by Zden2k
6 years ago

I finally did it by changing the css:

CSS
.codehead {
        font-family: Consolas;
        text-indent: 6px;
        color:#000000;
        background:#888888;
        border-top-left-radius:4px;
        border-top-right-radius:4px;
        padding: 2px 0px;
}

.codehead pre {
        text-indent: 0px;
        margin: 0 ;
}

and with html:

HTML
<div class="codehead">raw.py
<pre><code>#!/usr/bin/env python
print()
</code></pre></div>

How it looks like now:
code|268x117

But my question still remains, how to do it better with less work? I just imagine something like:

TXT
<code | raw.py>
#!/usr/bin/env python
print()
</code>

or something similar:

TXT
<code description="raw.py">
#!/usr/bin/env python
print()
</code>
👍 1
last edited 02/29/20 by Zden2k
6 years ago

Well, as it says, help yourself. I solve it by installing shortcode plugin with settings:

PHP
<?php
namespace Grav\Plugin\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
class StrikeShortcode extends Shortcode
{
    public function init()
    {
        $this->shortcode->getHandlers()->add('codehead', function(ShortcodeInterface $sc) {
            $codehead = $sc->getParameter('codehead', $this->getBbCode($sc));
            $buffer = $sc->getContent();
            $buffer  = preg_replace( "/(^\n|\n$)/", "", $buffer);
            $str_before = array("<", ">", "&ltp&gt", "&lt/p&gt");
            $str_after  = array("&lt", "&gt", "", "");
            $buffer = str_replace($str_before, $str_after, $buffer);
            return '<pre class="codehead">'.$codehead.'<code>'.$buffer.'</code></pre>';
        });
    }
}

And in default.md:

TXT
[codehead="/usr/local/bin/script.py"]
Heloo world!
[/codehead]

It's not probably the purest solution, but works fine to me. Please, If anyone know about better way, let me know.

Thanks.

👍 1
last edited 02/29/20 by Zden2k
6 years ago

To paraphrase a proverb:

give a man a fish and you feed him for a day; let him find out himself how to fish and you feed him for a lifetime

You might want to pick your preferred solution and mark it as 'solution'.

last edited 02/29/20 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 57 15 hours ago
Support · by Anna, 3 days ago
2 65 17 hours ago
Support · by Justin Young, 18 hours ago
1 33 18 hours ago
Support · by Duc , 1 week ago
2 68 5 days ago
Support · by Colin Hume, 1 week ago
2 60 6 days ago