blacken-docs icon indicating copy to clipboard operation
blacken-docs copied to clipboard

support for orgmode

Open ndvanforeest opened this issue 3 years ago • 1 comments

Org mode (via org babel) allows to include python source blocks. The python code can be edited right away in org mode, but it's a bit of pain to call blacken every time on each code block. (It must be doable in elisp, but for the moment I don't know how to do that). It's much more handy to run blacken-docs on the org file itself.

ndvanforeest avatar Jun 08 '22 21:06 ndvanforeest

I don't quite know what to do with the failed checks. In my second to last commit, I repaired the message of pre-commit, namely the long string. For the rest all works on my machine.

ndvanforeest avatar Jun 08 '22 21:06 ndvanforeest

That is a messy example, which I also missed.

The problem is that in org mode the noweb references in python code are written like this: <> and I don't quite know what syntax rules such code-ids have to follow. Simple rules like checking on letters will not suffice, because replacing the 5 in your example with a variable name is still valid code. I obviously don't want black to change the noweb references, nor the indentation. Now code ids will not appear (I think) at the right of an assignment (or something like that), but your example shows that a multiline criterion is necessary to distinguish between << 5 >> and <>.

For the moment I don't have a generic solution to tackle your example.

BTW, I apple the hack to my org files, and it works really well. But I don't write << 5 >> in my code :-)

On Fri, 19 Aug 2022 at 19:21, Anthony Sottile @.***> wrote:

@.**** commented on this pull request.

In blacken_docs.py https://github.com/asottile/blacken-docs/pull/168#discussion_r950416002:

@@ -178,6 +184,15 @@ def _latex_match(match: Match[str]) -> str: code = textwrap.indent(code, match['indent']) return f'{match["before"]}{code}{match["after"]}'

  • def _orgmode_match(match: Match[str]) -> str:
  •    code = textwrap.dedent(match['code'])
    
  •    code = re.sub(r'( *<<.*>>)', r'#\1', code)
    

this hack still isn't going to work -- this is valid code:

x = ( y << 5 >> 1 )

— Reply to this email directly, view it on GitHub https://github.com/asottile/blacken-docs/pull/168#pullrequestreview-1079136395, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAM266OJBZ45DTX2IXRU26LVZ67BBANCNFSM5YH3ZNQQ . You are receiving this because you authored the thread.Message ID: @.***>

ndvanforeest avatar Aug 19 '22 18:08 ndvanforeest