{"action":"created","issue":{"url":"https://api.github.com/repos/fimad/scalpel/issues/38","repository_url":"https://api.github.com/repos/fimad/scalpel","labels_url":"https://api.github.com/repos/fimad/scalpel/issues/38/labels{/name}","comments_url":"https://api.github.com/repos/fimad/scalpel/issues/38/comments","events_url":"https://api.github.com/repos/fimad/scalpel/issues/38/events","html_url":"https://github.com/fimad/scalpel/issues/38","id":163966633,"number":38,"title":"Getting nth element?","user":{"login":"aupiff","id":804266,"avatar_url":"https://avatars.githubusercontent.com/u/804266?v=3","gravatar_id":"","url":"https://api.github.com/users/aupiff","html_url":"https://github.com/aupiff","followers_url":"https://api.github.com/users/aupiff/followers","following_url":"https://api.github.com/users/aupiff/following{/other_user}","gists_url":"https://api.github.com/users/aupiff/gists{/gist_id}","starred_url":"https://api.github.com/users/aupiff/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aupiff/subscriptions","organizations_url":"https://api.github.com/users/aupiff/orgs","repos_url":"https://api.github.com/users/aupiff/repos","events_url":"https://api.github.com/users/aupiff/events{/privacy}","received_events_url":"https://api.github.com/users/aupiff/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/fimad/scalpel/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-07-05T23:11:59Z","updated_at":"2016-08-08T15:47:32Z","closed_at":null,"body":"If I'm working with html resembling the following,\r\n\r\n```html\r\n<table>\r\n  <tr>\r\n    <td><strong>col 1</strong></td>\r\n    <td><a href=\"special-link\">col 2</a></td>\r\n    <td>col k</td>\r\n  </tr>\r\n  <tr>\r\n    <td>col 1</td>\r\n    <td><a href=\"special-link\">col 2</a></td>\r\n    <td>col k</td>\r\n  </tr>\r\n  <tr>\r\n    <td><a href=\"link\">col 1</a></td>\r\n    <td><a href=\"special-link\">col 2</td>\r\n    <td>col k</td>\r\n  </tr>\r\n</table>\t\t\r\n```\r\n\r\nWhat is the suggested way to use Scalpel to extract the `n`th `td` from each `tr`? Notice that we can only be sure of the form of the column of interest, and that the other `td`s in each row have variable structure.\r\n\r\nI'm resorting to hacky solutions that look like:\r\n\r\n```Haskell\r\ntableScraper :: Scraper String [String]\r\ntableScraper = chroot (\"table\" :: String) $\r\n    chroots (\"tr\" :: String) $ (!! 1) <$> columnLinkScraper\r\n\r\ncolumnLinkScraper = chroots (\"td\" :: String) $\r\n    attr (\"href\" :: String) (\"a\" :: String) <|> return \"\"\r\n\r\nmain :: IO ()\r\nmain = hspec $ do\r\n\r\n  describe \"scalpel\" $\r\n    it \"parsed well\" $ do\r\n        contents <- readFile \"test.html\"\r\n        let scrapedLinks = scrapeStringLike contents tableScraper\r\n        case scrapedLinks of\r\n           Just links -> links `shouldBe` replicate (length links) \"special-link\"\r\n           Nothing -> scrapedLinks `shouldSatisfy` isJust\r\n```\r\n\r\nIs there a better way to do this? Does a `chrootIndex :: Int -> s -> Scraper str a -> Scraper str a` function make sense? `chroot` is essentially just `chrootHead`, after all. I've looked at the implementation of `chroot` and is seems like we would just need to use an alternative `chroots` that doesn't use the `mapMaybe` function and preserves the total number of matched root tags. I'm happy to write this function if everyone thinks it's a good idea."},"comment":{"url":"https://api.github.com/repos/fimad/scalpel/issues/comments/238279638","html_url":"https://github.com/fimad/scalpel/issues/38#issuecomment-238279638","issue_url":"https://api.github.com/repos/fimad/scalpel/issues/38","id":238279638,"user":{"login":"fimad","id":409115,"avatar_url":"https://avatars.githubusercontent.com/u/409115?v=3","gravatar_id":"","url":"https://api.github.com/users/fimad","html_url":"https://github.com/fimad","followers_url":"https://api.github.com/users/fimad/followers","following_url":"https://api.github.com/users/fimad/following{/other_user}","gists_url":"https://api.github.com/users/fimad/gists{/gist_id}","starred_url":"https://api.github.com/users/fimad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fimad/subscriptions","organizations_url":"https://api.github.com/users/fimad/orgs","repos_url":"https://api.github.com/users/fimad/repos","events_url":"https://api.github.com/users/fimad/events{/privacy}","received_events_url":"https://api.github.com/users/fimad/received_events","type":"User","site_admin":false},"created_at":"2016-08-08T15:47:32Z","updated_at":"2016-08-08T15:47:32Z","body":"I am hesitant about adding a predicate to `chroots` since its not very future friendly. If we decide that we'd like to be able to condition on some additional piece of metadata then it would be cumbersome to try to add that.\r\n\r\nI think the least intrusive way to add `position` would be to update `TagSpec` to include a type for ephemeral metadata. We could put the current match index (I'm also wondering if `position` is too generic a name and that maybe something like `matchIndex` or `matchOrder` wouldn't be more appropriate) in there now and it would also provide a place to shove future metadata that may change between select invocations.\r\n\r\nI think the easiest way to figure out the position would be in `select` to zip the result of `selectNodes` with `[0..]` and store that in the new TagSpec piece."}}
{"actor":{"display_login":"fimad"}}