Skip to content

Instantly share code, notes, and snippets.

@foofoodog
Last active September 20, 2015 15:04
Show Gist options
  • Save foofoodog/1fce92f80a227a48d51a to your computer and use it in GitHub Desktop.
Save foofoodog/1fce92f80a227a48d51a to your computer and use it in GitHub Desktop.
LINQPad to parse specific github README.md and generate reddit markdown TOC
var readme = "https://github.com/cyberkni/FolgertechManuals/blob/master/2020i3/README.md";
var includes = new[] { "id=\"user-content-", "href=\"#" };
var excludes = new[] { "<article" };
// input
IEnumerable<XElement> elements;
using (var client = new WebClient())
{
elements = from line in client.DownloadString(readme).Split('\n')
where
includes.All(include => line.Contains(include))
&& excludes.All(exclude => !line.Contains(exclude))
select XElement.Parse(line);
}
// output
var template = "[{0}]({1}{2})\n";
var links = from element in elements
let text = element.Value
let href = element.Descendants("a").First().Attribute("href").Value
let markdown = string.Format(template, text, readme, href)
let link = new Hyperlinq(new Uri(readme + href).ToString(), text)
select new { markdown, link };
links.Dump();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment