github.com/Kindred87/Obsidian@v0.0.0-20210809203756-86936424b848/server/adapters.go (about)

     1  package server
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/Kindred87/Obsidian/api"
     8  	"github.com/Kindred87/Obsidian/retrieval/html"
     9  )
    10  
    11  // htmlNodeHelperStringConv translates between retrieval and api HTMLNodeLists.
    12  func htmlNodeHelperStringConv(in []html.NodeList) (out []*api.HTMLNodeList) {
    13  	for _, a := range in {
    14  		out = append(out, &api.HTMLNodeList{
    15  			Name:  a.Name,
    16  			Nodes: toStringClass(a),
    17  		})
    18  	}
    19  	return out
    20  }
    21  
    22  // ToStrings translates a NodeList's nodes into single-line strings.
    23  func toStringClass(nList html.NodeList) []string {
    24  	toReturn := []string{}
    25  
    26  	for _, node := range nList.Nodes {
    27  		toAppend := strconv.Itoa(node.ID) + ": " + node.Name
    28  
    29  		if node.Class != "" {
    30  			// 11-len() is used instead of \t for gRPC clients without escape character support.
    31  			toAppend = toAppend + strings.Repeat(" ", 11-len(toAppend)) + "Class: " + node.Class
    32  		}
    33  
    34  		toReturn = append(toReturn, toAppend)
    35  	}
    36  
    37  	return toReturn
    38  }
    39  
    40  func htmlSearchStringConv(in []html.NodeList) (out []*api.HTMLNodeList) {
    41  	for _, a := range in {
    42  		out = append(out, &api.HTMLNodeList{
    43  			Name:  a.Name,
    44  			Nodes: toStringsData(a),
    45  		})
    46  	}
    47  	return out
    48  }
    49  
    50  func toStringsData(nList html.NodeList) []string {
    51  	toReturn := []string{}
    52  
    53  	for _, node := range nList.Nodes {
    54  		toAppend := strconv.Itoa(node.ID) + ": " + node.Data
    55  
    56  		toReturn = append(toReturn, toAppend)
    57  	}
    58  
    59  	return toReturn
    60  }