ch5ex4

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2022 License: GPL-3.0 Imports: 1 Imported by: 0

README

= Exercise 5.4
// Refs:
:url-base: https://github.com/fenegroni/TGPL-exercise-solutions
:workflow: workflows/Exercise 5.4
:action: actions/workflows/ch5ex4.yml
:url-workflow: {url-base}/{workflow}
:url-action: {url-base}/{action}
:badge-exercise: image:{url-workflow}/badge.svg?branch=main[link={url-action}]

{badge-exercise}

Extend the `Visit` function so that
it extracts other kinds of links from the document,
such as images, scripts, and style sheets.

== Test

The test should cover all element types that contain links.
These are elements with attributes `src` or `href`.

E.g. `<script>` and `<img>` use `src`, `<a>` and `<link>` use `href`.

`<link>` is used in `<head>` to point to a style sheet.

== Example

Given this example HTML document as input:

[source,html]
----
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <a href="link">a</a>
        <img src="imagelink">
        <script src="scriptlink"></script>
    </body>
</html>
----

the Example function's `Output` is:

[source,go]
----
// Output:
// style.css
// link
// imagelink
// scriptlink
----

Documentation

Overview

Exercise5.4 prints the links in an HTML document read from standard input.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Visit

func Visit(links []string, n *html.Node) []string

Visit appends to links each link found in n and returns the result.

Example
document := `<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <a href="link">a</a>
        <img src="imagelink">
        <script src="scriptlink"></script>
    </body>
</html>`
parseTree, _ := html.Parse(strings.NewReader(document))
for _, link := range Visit(nil, parseTree) {
	fmt.Println(link)
}
Output:

style.css
link
imagelink
scriptlink

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL