My latest attempt at this problem is a tool I call `cq` [1]. It's a way to query code with a selector, rather than line numbers. I use it for blog posts to pull code chunks into markdown.
It lets you write a (static) document that mixes prose and code in a way thats best for the reader. But 1. the code is runnable on disk and 2. you don't have to copy and paste.
Generally I find that literate programming solutions don't have good support for hiding portions of the code. This tends to limit the tool's applicability to trivial code examples.
I use `cq` to extract key code blocks from complete projects and then provide the whole project alongside the post.