SimpleVerbs.php

<?php

namespace Tlf\Scrawl\Ext\MdVerb;

class SimpleVerbs implements \Tlf\Scrawl\ExtensionType\MdVerbInterface {

    protected $scrawl;

    public function __construct($scrawl){
        $this->scrawl = $scrawl;
    }

    public function getVerbs(): array{
        return [
            'import'=>'at_import',
            'file' => 'at_file',
            'template'=> 'at_template',
            'easy_link'=> 'at_easy_link',
            'hard_link'=> 'at_hard_link',
            'see_file'=> 'at_see_file',
        ];
    }

    public function at_template(array $info, string $templateName, ...$templateArgs){
        $output = $this->scrawl->loadTemplate($this, $templateName, $templateArgs);
        return $output;
    }

    /**
     * Import something previously exported with @export or @export_start/@export_end
     * @usage @import(Namespace.Key)
     * @output whatever was exported by @export or @export_start/_end
     */
    public function at_import(array $info, string $key){
        $output = $this->scrawl->getOutput('key',$key);

        if ($output===null){
            $this->scrawl->error('@import', '@import('.$key.') failed in '.$info['file']->relPath);
            $replacement = '# Key not found for '.$key;//.trim($line);
        } else {
            $replacement = $output;
        }
        return $replacement;
    }

    /**
     *
     * @usage @file(rel/path/to/file.ext)
     * @output the file's content, `trim`med.
     */
    public function at_file(array $info, string $relFilePath){
        $file = $this->scrawl->dir.'/'.$relFilePath;

        if (!is_file($file)){
            $this->scrawl->error('@file', "@file($relFilePath) failed. File does not exist.");
            return "'$file' is not a file.";
        }

        return trim(file_get_contents($file));
    }

    /**
     *
     * @usage @see_file(relative/file/path)
     * @output `[relative/file/path](urlPath)`
     */
    public function at_see_file(array $info, string $relFilePath){
        $path = $this->scrawl->dir.'/'.$relFilePath;
        if (!is_file($path) && !is_dir($path)){
            $this->scrawl->error("@see_file","@see_file($relFilePath): File does not exist");
        }
        $urlPath = $relFilePath;
        if ($urlPath[0]!='/')$urlPath = '/'.$urlPath;
        $link = '['.$relFilePath.']('.$urlPath.')';
        return $link;
    }


    /** just returns a regular markdown link. In future, may check validity of link or do some kind of logging 
     *
     * @usage @hard_link(https://url.com, LinkName)
     * @output [LinkName](https://url.com)
     */
    public function at_hard_link(array $info, string $url, string $name=null){
        if ($name==null) $name = $url;
        return '['.$name.']('.$url.')';
    }

    /**
     *
     *
     * @usage @easy_link(twitter, TaelufDev)
     * @output [TaelufDef](https://twitter.com/TaelufDev)
     * @todo support a third param 'LinkName' so the target and link name need not be identical
     */ 
    public function at_easy_link(array $info, string $service, string $target){
        $sites = [
            'twitter'=>'https://twitter.com/',
            'gitlab'=>'https://gitlab.com/',
            'github'=>'https://github.com/',
            'facebook'=>'https://facebook.com/',
            'tlf'=>'https://tluf.me/',
        ];

        $host = $sites[strtolower($service)] ?? null;
        if ($host==null){
            $this->scrawl->error('@easy_link', "@easy_link($service,$target): Service '$service' is not valid. Options are "
                .implode(', ', array_keys($sites))
            );
            return "--service '$service' not found--";
        }
        $url = $host.$target;
        $linkName = $target;
        $mdLink = "[$linkName]($url)";
        return $mdLink;
    }

    public function getCurrentBranchForComposer(){
        $branch = exec("git branch --show-current");
        return $branch.'.x-dev';
    }

    public function getGitCloneUrl(){
        $url = exec("git config --get remote.origin.url | sed -r 's/.*(\\@|\\/\\/)([^:\\/]*)(\\:|\\/)(.*)\\.git/https:\\/\\/\\2\\/\\4/'");
        $url .= '.git';
        return $url;
    }

}