~ols/cramp

933cb02b9771e3aaba0592077629c4282dc5ef67 — Oliver Leaver-Smith 2 years ago eebe501
Add support for non-text mime types
1 files changed, 18 insertions(+), 6 deletions(-)

M cramp.go
M cramp.go => cramp.go +18 -6
@@ 2,6 2,7 @@ package main

import (
	"bufio"
	"bytes"
	"crypto/tls"
	"fmt"
	"io/ioutil"


@@ 80,7 81,18 @@ func main() {
						return
					}
					defer response.Body.Close()
					text, err := readability.New().Parse(response.Body, url)
					bodyBytes, _ := ioutil.ReadAll(response.Body)
					mimeType := http.DetectContentType(bodyBytes)
					mimere := regexp.MustCompile(`text/.*`)
					if !mimere.MatchString(mimeType) {
						returned = "20 " + mimeType + "\r\n" + string(bodyBytes)
						c.Write([]byte(returned))
						c.Close()
						return
					}
					fmt.Printf("Trying to read %s\n", url)
					body := bytes.NewReader(bodyBytes)	
					text, err := readability.New().Parse(body, url)
					if err != nil {
						returned = "20 text/gemini; charset=utf-8\r\n# Uh oh\nUnable to parse that page, sorry\n=> / Try another"
						c.Write([]byte(returned))


@@ 95,25 107,25 @@ func main() {
						CitationMarkers:     true,
						NumberedLinks:       true,
						LinkEmitFrequency:   0,
						EmitImagesAsLinks:   false,
						EmitImagesAsLinks:   true,
						ImageMarkerPrefix:   "‡",
						EmptyLinkPrefix:     ">>",
					}
					ctx := html2gemini.NewTraverseContext(options)
					fmt.Printf("Trying to gemtextify %s\n", url)
					gemtext, err := html2gemini.FromString(text.Content, *ctx)
					re := regexp.MustCompile(`=> http`)
					httpReplace := "=> " + root + "/?http"
					gemtext = re.ReplaceAllString(gemtext, httpReplace)
					if err != nil {
						returned = "20 text/gemini; charset=utf-8\r\n# Uh oh\nUnable to convert that site to Gemtext, sorry"
						c.Write([]byte(returned))
						c.Close()
						return
					}
					re := regexp.MustCompile(`=> http`)
					httpReplace := "=> " + root + "/?http"
					gemtext = re.ReplaceAllString(gemtext, httpReplace)
					returned = "20 text/gemini; charset=utf-8\r\n" + gemtext
					c.Write([]byte(returned))
					c.Close()

				}
			}
		}(conn)