Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute code block in markdown file #368

Open
linux-china opened this issue Oct 26, 2018 · 6 comments
Open

Execute code block in markdown file #368

linux-china opened this issue Oct 26, 2018 · 6 comments

Comments

@linux-china
Copy link
Contributor

linux-china commented Oct 26, 2018

justfile is very easy, but you know some guys still write code in markdown. For example as following:

```shell {#hello .just}
echo hello
```

With markdown attributes, and #hello is recipe name, and .just means to execute the code with just.

The above code is friendly with most editors, and no break.

Updated on 11/29/2023

@casey
Copy link
Owner

casey commented Oct 29, 2018

This is an interesting feature! I'm not totally sold on it, but I'll leave it open in case it's something that a lot of people want and would use.

@casey casey added this to the eventually milestone Apr 17, 2019
@runeimp
Copy link

runeimp commented Jan 9, 2020

@linux-china I love Markdown and have used it for years. But I've never seen anything execute code from a Markdown file. I think it's great for documenting code or creating page content for tools like Hugo, etc. but using it as source for some tool as well is cool sounding but instantly strikes me as impractical. Doesn't mean it's a bad idea I'm just not seeing the utility. Can you list any tools that have this sort of feature? And list a use case where this might be better than just targeting a script?

@casey casey removed this from the eventually milestone Jul 2, 2020
@chengxuncc
Copy link

chengxuncc commented Jan 13, 2021

Might be useful to introduce mask for whom want this feature.

@daniels
Copy link

daniels commented Nov 29, 2023

This was my first idea when I just now found out about just. I don't want to duplicate the information I already put into my README.md, but if just could pick up recipes defined in code blocks it would be really useful.

Unlike the original idea here, I would not have expected any different format with extra markdown parsing - just read recipes defined in code blocks marked with ```just. (Nice if multiple code blocks are accepted and merged.)

@linux-china
Copy link
Contributor Author

linux-china commented Nov 29, 2023

This was my first idea when I just now found out about just. I don't want to duplicate the information I already put into my README.md, but if just could pick up recipes defined in code blocks it would be really useful.

Unlike the original idea here, I would not have expected any different format with extra markdown parsing - just read recipes defined in code blocks marked with ```just. (Nice if multiple code blocks are accepted and merged.)

According to markdown attributes, and I think following code block is correct:

```shell {#hello .just}
echo hello
```

#hello is recipe name, and .just means to use just.

@laniakea64
Copy link
Contributor

laniakea64 commented Nov 29, 2023

I don't want to duplicate the information I already put into my README.md, but if just could pick up recipes defined in code blocks it would be really useful.
...
I would not have expected any different format with extra markdown parsing ... (Nice if multiple code blocks are accepted and merged.)

Should be doable already, here's quick attempt at working with the syntax @linux-china demonstrated:

readme_justfile := justfile_directory() / '.readme.just'

readme2justfile:
  #!/usr/bin/env python3
  import re
  recipeStartRx = re.compile(r'^```[a-z]+\s+\{#([a-zA-Z_][a-zA-Z0-9_-]*)\s+\.just\}', re.I)
  recipes = dict()
  with open('README.md', 'r') as readme:
    recipeName = None
    for line in readme:
      if recipeName:
        if line.strip() == '```':
          recipeName = None
        else:
          recipes[recipeName] += '  ' + line
      elif s := recipeStartRx.search(line):
        s=s.group(1)
        recipeName = s
        if recipeName not in recipes:
          recipes[recipeName] = ''
  with open("""{{readme_justfile}}""", 'w') as justfile:
    for recipe, body in recipes.items():
      justfile.write(f'{recipe}:\n{body}\n')

@r *recipe: readme2justfile
  {{quote(just_executable())}} -f {{quote(readme_justfile)}} {{recipe}}

Then you can run just r your_readme_recipe.

It would be more seamless if #1646 were implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants