From d6c895778e50fba6e55457ed2a168d4cbd5ce546 Mon Sep 17 00:00:00 2001 From: lomna-dev Date: Sun, 13 Aug 2023 18:33:31 +0530 Subject: [PATCH] First Commit --- main.html | 1303 +++++++++++++++++++++++++++++++++ main.org | 411 +++++++++++ main.tsk | 7 + src/export.el | 9 + src/htmlize.el | 1864 ++++++++++++++++++++++++++++++++++++++++++++++++ src/org.css | 177 +++++ 6 files changed, 3771 insertions(+) create mode 100644 main.html create mode 100644 main.org create mode 100644 main.tsk create mode 100644 src/export.el create mode 100644 src/htmlize.el create mode 100644 src/org.css diff --git a/main.html b/main.html new file mode 100644 index 0000000..fa94f9a --- /dev/null +++ b/main.html @@ -0,0 +1,1303 @@ + + + + + + + +Computer Networks + + + + + + + + +
+

Computer Networks

+ +

+


+

+
+

1. Introduction

+
+

+A computer network is collection of two or more computers that can communicate with each other. +

+ +

+Computer networks have two basic building blocks +

+
    +
  1. Nodes : these are devices that are connected to the network. They can either be network redistribution points or communication endpoints. Example of nodes are computers, routers, switches and other devices on the network.
  2. +
  3. Links : these are the connections between nodes. It includes the type of connectivity (wired or wireless) and the protocols that are used for communication between the nodes.
  4. +
+

+Computer networks are made of protocols which allow us to send data through links between nodes. +


+

+
+
+

1.1. Some basic terminologies

+
+

+Here, we will look at basic terminologies used in computer networks. Most of these will be expanded on further. +

+
    +
  • Protocol : a set of rules and standards that govern how data is transmitted between nodes in the network.
  • +
  • Topology : refers to the arrangement of links and nodes in a computer network. There are eight basic network topologies: point-to-point, ring or circular, mesh, tree, star, bus, daisy chain and hybrid
  • +
  • IP Address : it is a unique numerical identifier that is assigned to each node in the network. It's purpose is to identify devices in network allowing us to send and recieve data from them. IP Address stands for Internet Protocol Address.
  • +
  • DNS : stands for Domain Name System. It is naming system and a protocol which translates human redable domains (such as git.lomna.xyz) and translates it into an IP address of the node from which we need to get data.
  • +
  • Firewall : it is a security device which can either be hardware-based or software-based. It controls and monitors incoming and outgoing network traffic and protects from unauthorized access and security threats.
  • +
+ +

+A system which complies with all the protocols can be connected to a network +

+
    +
  1. Open System : a system which complies to all network protocols and can be used for communication in network.
  2. +
  3. Closed Sytem : if a system does not comply to all network protocols and thus is not connected to network, unable to communicate.
  4. +
+ +

+Computer networks can fall under two broad categories +

+
    +
  1. Client-Server Architecture : in this architecture a communicating node can either be a server or a client. The client sends requests to the server, the server fulfills that request and sends a response. There are multiple clients connected to the same server.
  2. +
  3. Peer-to-Peer : in this architecture there is no central server, tasks are divided amongst the nodes. Each node has the same set of capabilities and responsibilities.
  4. +
+

+


+

+
+
+
+

1.2. OSI Model

+
+

+OSI stands for Open Systems Interconnection. It is a reference model which specifies standards and communication protocols for computer network. It is a 7 layer architecture. Each layer has different functions and protocols. The seven layers are +

+
    +
  1. Physical Layer
  2. +
  3. Data Link Layer
  4. +
  5. Network Layer
  6. +
  7. Transport Layer
  8. +
  9. Session Layer
  10. +
  11. Presentation Layer
  12. +
  13. Application Layer
  14. +
+

+We don't use the OSI Model and mostly use it as a referencial model. The TCP/IP model is more widely used. +


+

+
+
+
+

1.3. Unique Identifiers of Computer Network

+
+

+In order to establish communication between two nodes, we need to identify the nodes. This is done by using unique identifiers given to devices on a network. +

+
+
+

1.3.1. Hostname

+
+

+A hostname is a human readable label that is assigned to a device on the network. On the internet, a hostname is a domain name which has been assigned to a host computer. It is useful to identify devices on a local network. +

+
+
+
+

1.3.2. IP Address

+
+

+Also known as the logical address, every device to which a direct communication can be established will have a unique IP address. Interned Assigned Numbers Authority (IANA) assigns and manages ip addresses globally. Previously, ipv4 (version 4) was used and was an address of 32 bits, hence there are \(2^{32}\) unique ipv4 addresses. Because there are limited number of ipv4 addresses, we are running out of them. A new version of ip addresses ipv6 is now implemented. It has addresses of size 128 bits. +

+
+
+
+

1.3.3. MAC Address

+
+

+Stands for Media Access Control Address. It is also known as the physical address. Every Network Interface Card (NIC) is assigned a unique identifier. These are assigned by the manufacturer and cannot be changed. The length of a MAC address is 48 bits. +


+

+
+
+
+
+

1.4. Ports

+
+

+A single system has multiple connections and network applications running. A port is a logical channel through which data can be sent or recieved. A port number is always associated with a network address such as an IP address so that we can identify the device whose port was referenced. The unique combination of ip address and port number together is called a socket, example 192.168.0.1:8000 +
+The ip address and port number are seperated by a colon (:) +

+ +

+The ports are divided into three categories. +

+ + + + +++ ++ + + + + + + + + + + + + + + + + + + + + + + +
Port TypeRange
Well known ports0 - 1023
Registered ports1024 - 49151
Dynamic ports49152 - 65535
+ +
    +
  • The ports from 0 through 1023 are called well-known ports or system ports. They are used by system processes to provide widely used network services. Example, port 20 is for FTP and 80 is for HTTP.
  • + +
  • Ports in the range from 1024 to 49151 are user or registered ports. These ports can be freely used by the user. Unlike system ports, most OS will allow applications to use these ports without superuser privelages.
  • + +
  • Ports in range 49152 to 65535 are dynamic ports or private ports. They are also called ephemeral ports, because they are used for very short period of time. The TCP, UDP and SCTP typically use ephemeral port for client end of communication. The allocation of ephemeral port is temporary and only valid for short duration of communication session.
  • +
+ +

+


+

+
+
+
+
+

2. Network Models

+
+

+A network model defines a way to organize a system's functions and features. It also defines it's structure and protocols. +

+ +

+In order to assign functionality to different parts of a network, these models are organized into layered structure. Each layer has a set of functions to perform. Protocols are created to handle functions in each layer. So each layer will have it's own collection of protocols which is called a protocol suite. +

+ +

+There are a few guiding principles for creating layers in a network model, such as +

+
    +
  • Not creating too many layers, as it may lead to unneccessary complexity.
  • +
  • Reduce the number of interactions between the layers
  • +
  • Collect similar functions and seperating fundamentally different functions in different layers
  • +
  • Indentify whether the function of layer is based on hardware or software.
  • +
+ +

+Lower layers are linked with hardware whereas, the upper layers are linked with software. +

+ +

+There have been many network models, many of them are no longer used. IBM's SNA model, Apple's AppleTalk and IPX/SPX model are all no longer in use. They were phased out by the popular and most commonly used model today, the TCP/IP model. +


+

+
+
+

2.1. OSI Model

+
+

+TODO : Add OSI model image +The OSI model is used as a reference model. This means that it is used to provide building blocks for other network models and compare other network models to improve connectivity and consistency. +

+ +

+The OSI model has 7 layers. Each layer has different functionality. The principles applied for the 7 layers are +

+
    +
  1. A layer should be created where a different abstraction is needed
  2. +
  3. Each layer should should perform a well-defined function
  4. +
  5. The function for each layer should be chosen with eye toward defining an internationally standardized protocols
  6. +
  7. Minimize the information flow across layer boundries
  8. +
  9. The number of layers should be large enough that distinct functions need not be in same layer, but small enough that architecture is not unwieldy
  10. +
+
+
+

2.1.1. Physical Layer

+
+

+The lowest layer of the OSI model. Responsible for physical connections between the devices. The physical layer handles data in form of bits and is responsible for transmitting bits without loss from one node to another. +

+ +

+Functions of physical layer are +

+
    +
  • Bit Synchronization : the physical layer provides a clok to synchronize sending and recieving of data between nodes
  • +
  • Bit rate control : the physical layer also controls the the number of bits sent per second.
  • +
  • Physical topologies : topology defines how the nodes and links are arranged in the network
  • +
  • Transmission mode : the physical layer defines how data will flow from one node to another. Some transmissions modes are simplex, half-duplex and full-duplex
  • +
+ +

+Hub, repeater, moden and cables are physical layer devices +

+
+
+
+

2.1.2. Data Link Layer (DLL)

+
+

+This layer is responsible for node-to-mode delivery of the message. It makes sure that data transfer is error-free from one node to another, over the physical layer. When packet enters a network, it is the responsibility of DLL to transmit it to the host using MAC address. +

+ +

+The DLL has two sublayers: +

+
    +
  1. Logical Link Control (LLC)
  2. +
  3. Media Access Control (MAC)
  4. +
+ +

+The packet recieved from network layer is divided into frames, depending on the frame size of NIC. The reciever's MAC address is obtained by placing an Address Resolution Protocol (ARP) request, asking for MAC address for given IP address. The destination host replies to the ARP request with it's MAC address. +

+ +

+The functions of DLL are +

+
    +
  • Framing : since physical layer only accepts and transmits stream of bits without meaning or structure, the DLL breaks message into recognizable frame with boundries. This is done by attaching special bit patterns at beginning and end of a frame. Breaking message into frames is called framing.
  • +
  • Physical addressing : after creating frames, the DLL will add MAC address of the sender and reciever in the header of each frame.
  • +
  • Error control : DLL will detect and retransmit damaged or lost frames.
  • +
  • Flow control : the data rate must be constant on both sides to not cause corruption in data. Flow control is coordinated by the DLL to avoid this.
  • +
  • Access contrl : when a single communication channel is shared by multiple devices, the MAC sub-layer of DLL determines which device has control over the channel at a given time.
  • +
+ +

+DLL is handled by the NIC and device drivers of host machines. +Switch and bridges are example of DLL devices. +

+
+
+
+

2.1.3. Network Layer

+
+

+The network layer controls operations of the subnet. +It is responsible for transmission of data from one host to other located on different subnets. It is also responsible for packet routing i.e, the selection of the shortest path for the packets. The sender and reciever IP addresses are place in the header by the network layer. +

+ +

+Routes can be based on static table that rarely changed, or are updated automatically at the start of conversation. Routes can also be highly dynamic to reflect the current network load. +

+ +

+Too many packets present in subnet can cause bottlenecks. Handling congestion is also the responsibility of network layer. The quality of service (delay, transit time, jitter, etc.) are also a network layer issue. +

+ +

+Functions of network layer are +

+
    +
  • Routing : the network layer determines which route will be used to send packet from sender to reciever.
  • +
  • Logical addressing : the network layer identifies the IP addresses of sender and reciever in the network an places them in the header.
  • +
+ +

+The implementation of network layer is in devices such as routers and switches. +

+
+
+
+

2.1.4. Transport Layer

+
+

+The data in transport layer is referred to as segments. This layer is responsible for end to end delivery of messages. It also acknowledges successful transmission and re-transmits data if error is found. +

+ +

+At sender's side, this layer recieves data from upper layers, performs segmentation and also implements flow and error control. It also adds source and destination port numbers in headers and forwards the segmented data to network layer. The destination port number is configured either by default or manually. example, when requesting web server, we use destination port 80 for the request because it is default port used for HTTP requests. +

+ +

+At reciever's side, it performs sequencing and reassembling of segmented data. Then port number is read from the header and data is forwarded to the respective application. +

+ +

+Transport layer is an end-to-end layer. It carries data all the way from source to destination. That is, a program on source machine carries on a conversation with similar program on destination machine using headers and control messages. The lower layers, have protocols which communicate between machine and immediate neighbours, not the source and destination. Layers 1 to 3 are chained, i.e. communicate with neighbours while layers 4 to 7 are end-to-end. +

+ +

+Funtions of transport layer are +

+
    +
  • Segmentation and Reassembly : at sender the message from session layer is broken into smaller units called segments, at receiver the segements are reassembled into message.
  • +
  • Service Point Addressing : the transport layer determines what type of service is provided to the session layer and ultimately, to the user. The type of service is determined when connection is established via port address.
  • +
+ +

+The service of transport layer is of two types +

+
    +
  1. Connection Oriented Service : it is a three phase service that has connection establishment, data transfer and then termination. The receiving node will send and acknowledgment and asks for re-transmission if there is error. This type of transmission is reliable and secure. example, TCP
  2. +
  3. Connectionless service : in this type of transmission, there is no need to establish a connection before data transfer and the receiver does not send an acknowledgement. This approach allows faster communication but is less reliable. example, UDP
  4. +
+ +

+Transport layer is operated by the OS. This layer is called the heart of OSI model. +

+
+
+
+

2.1.5. Session Layer

+
+

+This layer is responsible for establishing connection, maintanance of communication sessions and authentication. It ensures the security in connection. +

+ +

+The services provided by session layer are +

+
    +
  • Connection establishmen and release : The session layer open a connection between two nodes, this connection is called a session. In OSI, transport layer does not terminate the connection, the session layer is responsible for that. In TCP/IP model, this is done by the transport layer.
  • +
  • Dialogue control : in a session, there can be three different types of dialogue - two way simultaneous (full-duplex), two way alternate (half-duplex) and one way (simplex). It controls which side has the turn during communication.
  • +
  • Synchronization points and resynchronization : this layer allows the two sides to insert synchronization points between dialogue and allow them to resynchronize to a previous synchronization point, aborting the current transmission. This can be used for real-time audio/video.
  • +
+
+
+
+

2.1.6. Presentation Layer

+
+

+Also called the translation layer. This layer translates files and data from local formats to standard, transmittable formats. +

+ +

+The services provided by presentation layer are +

+
    +
  • Data conversion : example, converting file formats such as bmp to webp which are easier to transmit in a network.
  • +
  • Character code translation : changing strings to bits that can be transmitted.
  • +
  • Compression : compressing and decompressing data in order to reduce the amount of data has to be sent.
  • +
  • Encryption and Decryption : encrypting and decryting of messages for security
  • +
  • Serialization : converting complex data structures into flat structures using mechanisms like JSON and XML.
  • +
+
+
+
+

2.1.7. Application Layer

+
+

+Also called the Desktop Layer. This layer acts as the window for applications to access the network. This layer produces the data which will be transferred over network and displays the information to user. This layer also contains the protocols for interfaces to different functionalities of the web. +

+ +

+This layer has protocols such as HTTP, FTP, SMTP, DNS and provides the functionality to use services associated with these protocols. +


+

+
+
+
+
+

2.2. TCP/IP Model

+
+

+TCP/IP was used in ARPANET and now the worldwide Internet. The ARPANET started as a network of hundreds of universities and government installations. When satellite and radio networks were added, they caused problems with existing protocols. This need for a model that can connect multiple networks in a seamless way was the major goal of the TCP/IP model. +

+ +

+Another requirment was that connections remain intact as long as the source and destination machines were working, even if some machines or transmission lines were out of operation. So a flexible and robust network was needed. +

+ +

+The TCP/IP model had 4 layers, but sometimes physical layer is also included in the model. +

+
+ +
+

2.2.1. Link layer

+
+

+The requirnment for a robust network led to the choice of packet-switching network based on a connectionless layer that runs across different networks. The link layer describes what links like serial lines and classic ethernet must do to meet needs of this connectionless internet layer. It is not a layer in normal sense, but rather an interface between different hosts and transmission links. +

+
+
+ +
+

2.2.2. Internet layer

+
+

+This layer roughly corresponds to the OSI network layer. Its job is to permit hosts to inject packets into any network and have them travel independently to the destination. That is, it will allow packets from go to one network to another while they are going to destination. This property that packets can travel through any network makes the whole network more robust, by allowing communication even if a few networks in some path are down. +

+ +

+The packets may even arrive in a completely different order than in which they were sent. If in-order delivery was needed, then it's the responsibility of higher layers to rearrange them. +

+ +

+It is similar to the old mail system. When a sequence of international letters are dropped in a mailbox from one country, most mails will be delivered to the correct address in destination country. The mails may not go in the same sequence they we entered in the mailbox. Furthermore, each country (network for our analogy) has it's own stamps, envelope size, and other rules and protocols. +

+ +

+The internet layer defines an official packet format and protocol called Internet Protocol (IP), with a companion protocol Internet Control Message Protocol (ICMP) that helps its function. +

+ +

+The job of internet layer is to deliver IP packets where they are supposed to go. Packet routing is the major obstacle for this layer. (IP has proven not effective to avoid congestion) +

+
+
+
+

2.2.3. Transport layer

+
+

+TODO : Here +

+
+
+
+

2.2.4. Application layer

+
+

+TODO : Here +


+

+
+
+
+
+
+

3. Physical Layer

+
+

+The lowest layer of the TCP/IP model. It describes the standard for the physical connections between nodes. It only views the data as a stream of bits and is concerned with transmission of bits without error. +


+

+
+
+

3.1. Network Topology

+
+

+The arrangement of nodes and links in a computer network is called network topology. There are various types of topologies +

+ +

+TODO : Add images of network topology +

+
+
+

3.1.1. Point-to-Point

+
+

+It is the simplest connection with two nodes and a single link between them. +

+
+
+
+

3.1.2. Daisy chaining

+
+

+Also called linear topology, the nodes are connected in a series. Message will follows the whole chain till it reaches the destination. If a link fails in this topology, it is hard to find it, therefore it is not good for large networks. +

+
+
+
+

3.1.3. Mesh topology

+
+

+Every node is connected to another node through a direct link. In mesh topology, we use AHCP (Ad Hoc Configuration Protocol) and DHCP (Dynamic Host Configuration Protocol). +

+ +

+So every node is connected to every other node directly connected through a link. So if there are \(N\) nodes, then the each node has \((N - 1)\) links. +

+ +

+If there are \(N\) nodes, then in a mesh topology, the total number of links will be +\[ \text{number of links in mesh topology : } \frac{N(N - 1)}{2} \] +\[ \text{where } N \text{ is the number of nodes} \] +

+ +

+Advantages of mesh topology are +

+
    +
  • communication is fast between any pair of nodes
  • +
  • data transfer is reliable since there are dedicated links between all nodes
  • +
  • since every pair of node has dedicated link, this provides security and privacy
  • +
  • the network is more robust single a failure of single device won't break the network
  • +
+ +

+Drawbacks of mesh topology are +

+
    +
  • installation is hard, since each link needs to be configured
  • +
  • excessive amount of cables and channels are required to connect all nodes
  • +
  • the cost of maintanance is very high
  • +
+ +

+Mesh topology is of two types +

+
    +
  1. Full mesh topology : it is the traditional mesh topology, where every node is connected with other node thus every node has \((N - 1)\) connections.
  2. +
  3. Partial mesh topology : a more practical implementation where it is not necessary to connect all nodes to one another. Only channels with high amount of traffic are connected. This is not the traditional way to implement mesh topology.
  4. +
+
+
+
+

3.1.4. Star topology

+
+

+All nodes are connected to a central node (called the hub) through links. Hubs are not communication endpoints, their function is to forward packets across all ports. The hubs can either be passive in nature or active. +

+ +

+Active hubs repeat and strengthen incoming transmissions, while passive hubs simply serve as a point of connectivity. Active hubs are also referred to as repeaters. +

+ +

+Advantages of star topology are +

+
    +
  • only \(N\) links are required to connect \(N\) nodes to a network
  • +
  • each device will need only one port
  • +
  • if one link fails, it won't affect other links
  • +
  • cost effictive
  • +
+ +

+Drawback of star topology are +

+
    +
  • if the hub fails, then whole network is down
  • +
  • performace is dependant on the hubs
  • +
+
+
+ +
+

3.1.5. Bus topology

+
+

+In bus topology, all the nodes are connected to a single backbone line. This backbone is bi-directional, there are lines from node to backbone called droplines. This topology is not robust, since if backbone is damaged, the whole network is down. +

+ +

+In this topology, various MAC protocols are followed like TDMA, Pure Aloha, CDMA, etc. +

+ +

+Advantages of Bus topology +

+
    +
  • there is only a single link and \(N\) drop lines required
  • +
  • reduces the cost for installation
  • +
  • CSMA is the most common method for this topology
  • +
+ +

+Drawbacks +

+
    +
  • if backbone fails, then whole network is down
  • +
  • this topology can't handle heavy traffic
  • +
  • adding new nodes slows the network
  • +
  • security is very lower
  • +
+
+
+
+

3.1.6. Ring topology

+
+

+A daisy chain in a closed circular loop is called a ring topology. The most common method for communication in this topology is token passing. A token is a frame which is circulated around the network. The token is passed from one node to another till it reaches its destination. +

+ +

+Advantages +

+
    +
  • better for traffic than bus topology
  • +
  • cheap to install and expand
  • +
+ +

+Disadvantages +

+
    +
  • single node failure can cause entire network to fail
  • +
  • troubleshooting is difficult
  • +
  • the topology is not secure
  • +
+
+
+ +
+

3.1.7. Tree topology

+
+

+Also called a star-bus topology. It can be seen as multiple star topologies connected via a backbone like in bus topology. Tree topology is hierarchical and there are parent and child star networks. It uses protocols like DHCP and SAC. The backbone is like the truck of the tree and various star networks branch out from it. +

+ +

+The hubs connected directly to main backbone are central hubs. The hubs connected to other hubs are child hubs. This makes this topology very flexible. +

+ +

+Advantages +

+
    +
  • allows networks to prioritize different computers
  • +
  • new nodes are easy to add
  • +
  • error detection and error correction is easy
  • +
+ +

+Drawbacks +

+
    +
  • if backbone fails the entire network is down. If a parent hub fails, then all the connected child network is down
  • +
  • cost of maintanance is high
  • +
  • reconfiguration is hard
  • +
+
+
+ +
+

3.1.8. Hybrid topology

+
+

+A hybrid topology is when a network combines two or more topologies in a way that does not resemble standard topologies. +

+ +

+Advantages +

+
    +
  • this is very flexible
  • +
  • size of network can be easily expanded
  • +
+ +

+Disadvantages +

+
    +
  • it is challenging to design and maintain
  • +
  • the infrastructure needs specialization to maintain
  • +
+

+


+

+
+
+
+
+

3.2. Digital Modulation

+
+

+Wires carry analog signals in form of continuously varying voltage. To send digital information, we need to devise analog signals to represent bits. This process of converting bits to analog signals is called digital modulation. +

+
+
+

3.2.1. Bandwidth and maximum data rate

+
+

+Bandwidth is the measure of carrying capacity of a medium. It is measured in Hertz (Hz). +
+Note : in many places, maximum data rate is also refered to as the bandwidth. +

+
+
+
3.2.1.1. Maximum data rate for noiseless wires
+
+

+Suppose our analog signal has \(V\) different levels. Then for a noiseless wire with bandwidth of \(B\), the maximum data rate is +\[ \text{maximum data rate} = 2\ B\ log_2V \text{ bits/sec} \] +When converting bits, if we are only using two levels, one for 0 bit and one for 1 bit. So, we can in most cases simplify the formula to +\[ \text{maximum data rate} = 2\ B \text{ bits/sec} \] +Example, a noiseless 3-kHz channel cannot transmit binary signal (two levels) at rate exceeding 6000 bps. +

+
+
+
+
3.2.1.2. Maximum data rate for noisy wires
+
+

+But the noiseless wire is an ideal case, which is not possible in real wires. Amount of noise is measured using ratio of signal power to noise power called SNR (Signal-to-Noise Ratio). \(S\) denotes signal power and \(N\) denotes noise power. The ratio is expressed on a log scale as +\[ SNR = 10\ log_{10} (S/N) \text{ dB} \] +The unit of SNR is decibals (dB) +

+ +

+Using SNR, the maximum data rate in a noisy medium is +\[ \text{maximum data rate} = B\ log_2(1 + S/N) \text{ bits/sec} \] +Example, suppose SNR is of 40 dB and bandwidth is 1MHz. Then, using formula for SNR, \(S/N\) is \(10^4\). Therefore, the maximum data rate is +\[ \text{maximum data rate} = 1\ log_2 (10^4) \text{ Mbps}\] +\[ \text{maximum data rate} = 13.288 \text{ Mbps} \] +

+
+
+
+ +
+

3.2.2. Non-Return to Zero (NRZ)

+
+

+TODO : Add image +The most straightforward form of digital modulation is to use positive voltage for 1 bit and negative voltage to represent 0 bit. This scheme is called NRZ (Non-Return-to-Zero) +

+ +

+The NRZ signal propogates down the wire. At the other end, the reciever converts in into bits by sampling the signal at regular intervals of time. This signal won't look the same as one that was sent. It will be distorted by noise. To decode bits receiver will map signal samples to closest symbols. +

+ +

+The NRZ is simple, but is seldom used by itself in practice. We use more complex schemes to convert bits to signals. These schemes are called line codes. +

+
+
+ +
+

3.2.3. Increasing bandwidth efficiency by symbols

+
+

+Bandwidth is a limited resource, therefore we must use it efficiently. +

+ +

+With NRZ, the signal may cycle between positive and negative level every 2 bits (in case of alternating 1s and 0s). Therefore, atleast a bandwidth of B/2 Hz is needed when bit rate is B bits/sec. +

+ +

+One strategy for using the limited bandwidth is to increase the signaling levels. For example, by using four voltages, we can send 2 bits at once as a single symbol. When the number of levels is 4, the maximum data rate increases. +\[ \text{maximum data rate} = 4\ B \text{ bits/sec} \] +So, by increasing the number of levels, we can send symbols rather than bits to increase performance. +

+ +

+We call rate at which symbol changes the symbol rate to distinguish it from bit rate. An older name for symbol rate is baud rate. +\[ \text{bit rate} = \text{symbol rate } \times \text{ bits in a symbol} \] +Note: the number of levels does not need to be a power of 2. +

+
+
+
+

3.2.4. Clock recovery

+
+

+Clocks on both sender and reciever side are used to decode the incoming signal into bits. Suppose we are sending a signal with 20 consecutive zero's. In this case it will become hard to tell bits apart, 20 zero's will look very similar to 19 or 21 zero's. +

+ +

+We could use accurate clocks for this problem. But when bit rate is in multiple Mbps, the clock would need to be accurate down to less than a fraction of microsecond. Therefore, accurate clocks are not a general solution. +

+
+
+
3.2.4.1. Manchester encoding
+
+

+TODO : Add image +A clever trick is to mix a clock signal with the data signal by XORing them together. This scheme is called Manchester Encoding. +

+ +

+The clock should make a complete cycle for every bit that needs to be transmitted. Therefore, clock frequency needs to be twice of bit rate. In other words, if the bit rate is \(B\) bits/sec, then the clock frequency will be \(B\) Hz. +

+ +

+Therefore, the downside of manchester encoding is that it requires twice as much bandwidth as NRZ. +

+
+
+
+
3.2.4.2. Non-Return-to-Zero Inverted (NRZI)
+
+

+TODO : Add image +

+ +

+Clock recovery problems occur when there are long runs of 1's or 0's. NRZ-I encoding produces signals that are easy to translate with clock. +

+ +

+In NRZI, rather than using levels to represent bits, we use transitions to represent bits. Example, 1 bit will be a transition and 0 bit will be no transition. A transition is going from one level to another. +

+ +

+USB uses NRZI encoding to send bits. Long runs of 0 will still cause problems in NRZI. +

+
+
+
+
3.2.4.3. 4B/5B
+
+

+To truly fix the issue, we can break long runs of 0's by mapping group of bits to longer patterns that do not have consecutive 0's. +

+ +

+A well known code of this type is 4B/5B. Every 4 bits are mapped to 5 bit patterns which are then sent as analog signals. The translation table is fixed. +

+ +

+The 5 bit patterns are designed in a way that there are never more than three consecutive 0's. This encoding will add 25% overhead which is better than 100% overhead of manchester. +

+ + + +++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Data (4B)Codeword (5B)
000011110
000101001
001010100
001110101
010001010
010101011
011001110
011101111
100010010
100110011
101010110
101110111
110011010
110111011
111011100
111111101
+

+Since there are 32 different 5 bit patterns and all of them are not used. We can have remaining patterns which don't have many successive 0's as control signals. +

+ +

+4B/5B is usually used in conjunction to NRZI. +

+
+
+
+
3.2.4.4. Scrambling
+
+

+Another approach is scrambling the bits to reduce chances of long runs of 0's. +

+ +

+A scrambler will XOR the data with a psuedorandom sequence before it is transmitted. This mixing will make data pseudorandom (assuming the sequence was chosen independent of data). The receiver can XOR the incoming bits with same sequence to recover the real data. Therefore, the pseudorandom sequence must be easy to create. It is usually given as seed to a simple random number generator. +

+ +

+Scrambling will add no overhead. Also, repetitive data patterns can cause electromagnetic interference. Pseudorandom data is usually "white", i.e. has more uniform spread across signals reducing interference. +

+ +

+However, since scrambling is pseudorandom there is no guarentee that long runs won't occur. With predictable data patterns, it is easy for malicious users to send data that can cause long runs of 0's, causing link to fail. Earlier standards had this defect, it was possible to send certain "killer packets" that caused problems. +

+
+
+
+
+

3.2.5. Balanced signals

+
+

+Signals that have almost as many positive voltages as negative voltages are called balanced signals. Balanced signals average to zero, which means they have no DC electrical component. +

+ +

+We need balanced signals because +

+
    +
  • Many mediums such as coaxial cables or lines with transformers strongly reduce the DC component of signals.
  • +
  • A method of connecting receiver called capacitive coupling can only pass AC signals.
  • +
  • If signal is not balanced, we waste energy because DC component is filtered out.
  • +
  • Balanced signals have easier clock recovery since there is a mix of positive and negative voltages.
  • +
  • It helps in calibration of receivers as they can check average of signals to decode symbols. In unbalanced signals, the average drifts causing errors.
  • +
+ +

+


+

+
+
+
+
+
+
+

Author: Anmol Nawani

+

Created: 2023-08-13 Sun 18:07

+

Validate

+
+ + diff --git a/main.org b/main.org new file mode 100644 index 0000000..ba12ffe --- /dev/null +++ b/main.org @@ -0,0 +1,411 @@ +#+TITLE: Computer Networks +#+AUTHOR: Anmol Nawani +#+OPTIONS: H:4 +#+html_head: + +@@html:
@@ +* Introduction +A computer network is collection of two or more computers that can communicate with each other. + +Computer networks have two basic building blocks +1. *Nodes* : these are devices that are connected to the network. They can either be network redistribution points or communication endpoints. Example of nodes are computers, routers, switches and other devices on the network. +2. *Links* : these are the connections between nodes. It includes the type of connectivity (wired or wireless) and the protocols that are used for communication between the nodes. +Computer networks are made of protocols which allow us to send data through links between nodes. +@@html:
@@ +** Some basic terminologies +Here, we will look at basic terminologies used in computer networks. Most of these will be expanded on further. ++ *Protocol* : a set of rules and standards that govern how data is transmitted between nodes in the network. ++ *Topology* : refers to the arrangement of links and nodes in a computer network. There are eight basic network topologies: point-to-point, ring or circular, mesh, tree, star, bus, daisy chain and hybrid ++ *IP Address* : it is a unique numerical identifier that is assigned to each node in the network. It's purpose is to identify devices in network allowing us to send and recieve data from them. IP Address stands for Internet Protocol Address. ++ *DNS* : stands for Domain Name System. It is naming system and a protocol which translates human redable domains (such as [[https://git.lomna.xyz/][git.lomna.xyz]]) and translates it into an IP address of the node from which we need to get data. ++ *Firewall* : it is a security device which can either be hardware-based or software-based. It controls and monitors incoming and outgoing network traffic and protects from unauthorized access and security threats. + +A system which complies with all the protocols can be connected to a network +1. *Open System* : a system which complies to all network protocols and can be used for communication in network. +2. *Closed Sytem* : if a system does not comply to all network protocols and thus is not connected to network, unable to communicate. + +Computer networks can fall under two broad categories +1. *Client-Server Architecture* : in this architecture a communicating node can either be a server or a client. The client sends requests to the server, the server fulfills that request and sends a response. There are multiple clients connected to the same server. +2. *Peer-to-Peer* : in this architecture there is no central server, tasks are divided amongst the nodes. Each node has the same set of capabilities and responsibilities. +@@html:
@@ +** OSI Model +OSI stands for Open Systems Interconnection. It is a reference model which specifies standards and communication protocols for computer network. It is a 7 layer architecture. Each layer has different functions and protocols. The seven layers are +1. Physical Layer +2. Data Link Layer +3. Network Layer +4. Transport Layer +5. Session Layer +6. Presentation Layer +7. Application Layer +We don't use the OSI Model and mostly use it as a referencial model. The TCP/IP model is more widely used. +@@html:
@@ +** Unique Identifiers of Computer Network +In order to establish communication between two nodes, we need to identify the nodes. This is done by using unique identifiers given to devices on a network. +*** Hostname +A hostname is a human readable label that is assigned to a device on the network. On the internet, a hostname is a domain name which has been assigned to a host computer. It is useful to identify devices on a local network. +*** IP Address +Also known as the logical address, every device to which a direct communication can be established will have a unique IP address. Interned Assigned Numbers Authority (IANA) assigns and manages ip addresses globally. Previously, ipv4 (version 4) was used and was an address of 32 bits, hence there are $2^{32}$ unique ipv4 addresses. Because there are limited number of ipv4 addresses, we are running out of them. A new version of ip addresses ipv6 is now implemented. It has addresses of size 128 bits. +*** MAC Address +Stands for Media Access Control Address. It is also known as the physical address. Every Network Interface Card (NIC) is assigned a unique identifier. These are assigned by the manufacturer and cannot be changed. The length of a MAC address is 48 bits. +@@html:
@@ +** Ports +A single system has multiple connections and network applications running. A port is a logical channel through which data can be sent or recieved. A port number is always associated with a network address such as an IP address so that we can identify the device whose port was referenced. The unique combination of ip address and port number together is called a *socket*, example 192.168.0.1:8000 +\\ +The ip address and port number are seperated by a colon (:) + +The ports are divided into three categories. + +#+ATTR_HTML: :rules all +| Port Type | Range | +|------------------+---------------| +| Well known ports | 0 - 1023 | +| Registered ports | 1024 - 49151 | +| Dynamic ports | 49152 - 65535 | + ++ The ports from 0 through 1023 are called well-known ports or system ports. They are used by system processes to provide widely used network services. Example, port 20 is for FTP and 80 is for HTTP. + ++ Ports in the range from 1024 to 49151 are user or registered ports. These ports can be freely used by the user. Unlike system ports, most OS will allow applications to use these ports without superuser privelages. + ++ Ports in range 49152 to 65535 are dynamic ports or private ports. They are also called ephemeral ports, because they are used for very short period of time. The TCP, UDP and SCTP typically use ephemeral port for client end of communication. The allocation of ephemeral port is temporary and only valid for short duration of communication session. + +@@html:
@@ +* Network Models +A network model defines a way to organize a system's functions and features. It also defines it's structure and protocols. + +In order to assign functionality to different parts of a network, these models are organized into layered structure. Each layer has a set of functions to perform. Protocols are created to handle functions in each layer. So each layer will have it's own collection of protocols which is called a /protocol suite/. + +There are a few guiding principles for creating layers in a network model, such as ++ Not creating too many layers, as it may lead to unneccessary complexity. ++ Reduce the number of interactions between the layers ++ Collect similar functions and seperating fundamentally different functions in different layers ++ Indentify whether the function of layer is based on hardware or software. + +Lower layers are linked with hardware whereas, the upper layers are linked with software. + +There have been many network models, many of them are no longer used. IBM's SNA model, Apple's AppleTalk and IPX/SPX model are all no longer in use. They were phased out by the popular and most commonly used model today, the TCP/IP model. +@@html:
@@ +** OSI Model +TODO : Add OSI model image +The OSI model is used as a reference model. This means that it is used to provide building blocks for other network models and compare other network models to improve connectivity and consistency. + +The OSI model has 7 layers. Each layer has different functionality. The principles applied for the 7 layers are +1. A layer should be created where a different abstraction is needed +2. Each layer should should perform a well-defined function +3. The function for each layer should be chosen with eye toward defining an internationally standardized protocols +4. Minimize the information flow across layer boundries +5. The number of layers should be large enough that distinct functions need not be in same layer, but small enough that architecture is not unwieldy +*** Physical Layer +The lowest layer of the OSI model. Responsible for physical connections between the devices. The physical layer handles data in form of bits and is responsible for transmitting bits without loss from one node to another. + +Functions of physical layer are ++ *Bit Synchronization* : the physical layer provides a clok to synchronize sending and recieving of data between nodes ++ *Bit rate control* : the physical layer also controls the the number of bits sent per second. ++ *Physical topologies* : topology defines how the nodes and links are arranged in the network ++ *Transmission mode* : the physical layer defines how data will flow from one node to another. Some transmissions modes are simplex, half-duplex and full-duplex + +Hub, repeater, moden and cables are physical layer devices +*** Data Link Layer (DLL) +This layer is responsible for node-to-mode delivery of the message. It makes sure that data transfer is error-free from one node to another, over the physical layer. When packet enters a network, it is the responsibility of DLL to transmit it to the host using MAC address. + +The DLL has two sublayers: +1. Logical Link Control (LLC) +2. Media Access Control (MAC) + +The packet recieved from *network layer* is divided into frames, depending on the frame size of NIC. The reciever's MAC address is obtained by placing an Address Resolution Protocol (ARP) request, asking for MAC address for given IP address. The destination host replies to the ARP request with it's MAC address. + +The functions of DLL are ++ *Framing* : since physical layer only accepts and transmits stream of bits without meaning or structure, the DLL breaks message into recognizable frame with boundries. This is done by attaching special bit patterns at beginning and end of a frame. Breaking message into frames is called framing. ++ *Physical addressing* : after creating frames, the DLL will add MAC address of the sender and reciever in the header of each frame. ++ *Error control* : DLL will detect and retransmit damaged or lost frames. ++ *Flow control* : the data rate must be constant on both sides to not cause corruption in data. Flow control is coordinated by the DLL to avoid this. ++ *Access contrl* : when a single communication channel is shared by multiple devices, the MAC sub-layer of DLL determines which device has control over the channel at a given time. + +DLL is handled by the NIC and device drivers of host machines. +Switch and bridges are example of DLL devices. +*** Network Layer +The network layer controls operations of the subnet. +It is responsible for transmission of data from one host to other located on different subnets. It is also responsible for packet routing i.e, the selection of the shortest path for the packets. The sender and reciever IP addresses are place in the header by the network layer. + +Routes can be based on static table that rarely changed, or are updated automatically at the start of conversation. Routes can also be highly dynamic to reflect the current network load. + +Too many packets present in subnet can cause bottlenecks. *Handling congestion is also the responsibility of network layer*. The *quality of service* (delay, transit time, jitter, etc.) are also a network layer issue. + +Functions of network layer are ++ *Routing* : the network layer determines which route will be used to send packet from sender to reciever. ++ *Logical addressing* : the network layer identifies the IP addresses of sender and reciever in the network an places them in the header. + +The implementation of network layer is in devices such as routers and switches. +*** Transport Layer +The data in transport layer is referred to as *segments*. This layer is responsible for end to end delivery of messages. It also acknowledges successful transmission and re-transmits data if error is found. + +At sender's side, this layer recieves data from upper layers, performs *segmentation* and also implements *flow and error control*. It also adds source and destination port numbers in headers and forwards the segmented data to network layer. The destination port number is configured either by default or manually. example, when requesting web server, we use destination port 80 for the request because it is default port used for HTTP requests. + +At reciever's side, it performs sequencing and reassembling of segmented data. Then port number is read from the header and data is forwarded to the respective application. + +Transport layer is an *end-to-end layer*. It carries data all the way from source to destination. That is, a program on source machine carries on a conversation with similar program on destination machine using headers and control messages. The lower layers, have protocols which communicate between machine and immediate neighbours, not the source and destination. /*Layers 1 to 3 are chained, i.e. communicate with neighbours while layers 4 to 7 are end-to-end.*/ + +Funtions of transport layer are ++ *Segmentation and Reassembly* : at sender the message from session layer is broken into smaller units called segments, at receiver the segements are reassembled into message. ++ *Service Point Addressing* : the transport layer determines what type of service is provided to the session layer and ultimately, to the user. The type of service is determined when connection is established via port address. + +The service of transport layer is of two types +1. *Connection Oriented Service* : it is a three phase service that has connection establishment, data transfer and then termination. The receiving node will send and acknowledgment and asks for re-transmission if there is error. This type of transmission is reliable and secure. example, TCP +2. *Connectionless service* : in this type of transmission, there is no need to establish a connection before data transfer and the receiver does not send an acknowledgement. This approach allows faster communication but is less reliable. example, UDP + +Transport layer is operated by the OS. This layer is called the *heart of OSI model*. +*** Session Layer +This layer is responsible for establishing connection, maintanance of communication sessions and authentication. It ensures the security in connection. + +The services provided by session layer are ++ *Connection establishmen and release* : The session layer open a connection between two nodes, this connection is called a *session*. In OSI, transport layer does not terminate the connection, the session layer is responsible for that. In TCP/IP model, this is done by the transport layer. ++ *Dialogue control* : in a session, there can be three different types of dialogue - two way simultaneous (full-duplex), two way alternate (half-duplex) and one way (simplex). It controls which side has the turn during communication. ++ *Synchronization points and resynchronization* : this layer allows the two sides to insert synchronization points between dialogue and allow them to resynchronize to a previous synchronization point, aborting the current transmission. This can be used for real-time audio/video. +*** Presentation Layer +Also called the translation layer. This layer translates files and data from local formats to standard, transmittable formats. + +The services provided by presentation layer are ++ *Data conversion* : example, converting file formats such as bmp to webp which are easier to transmit in a network. ++ *Character code translation* : changing strings to bits that can be transmitted. ++ *Compression* : compressing and decompressing data in order to reduce the amount of data has to be sent. ++ *Encryption and Decryption* : encrypting and decryting of messages for security ++ *Serialization* : converting complex data structures into flat structures using mechanisms like JSON and XML. +*** Application Layer +Also called the Desktop Layer. This layer acts as the window for applications to access the network. This layer produces the data which will be transferred over network and displays the information to user. This layer also contains the protocols for interfaces to different functionalities of the web. + +This layer has protocols such as HTTP, FTP, SMTP, DNS and provides the functionality to use services associated with these protocols. +@@html:
@@ +** TCP/IP Model +TCP/IP was used in ARPANET and now the worldwide Internet. The ARPANET started as a network of hundreds of universities and government installations. When satellite and radio networks were added, they caused problems with existing protocols. This need for a model that can connect multiple networks in a seamless way was the major goal of the TCP/IP model. + +Another requirment was that connections remain intact as long as the source and destination machines were working, even if some machines or transmission lines were out of operation. So a flexible and robust network was needed. + +The TCP/IP model had 4 layers, but sometimes physical layer is also included in the model. + +*** Link layer +The requirnment for a robust network led to the choice of packet-switching network based on a connectionless layer that runs across different networks. The link layer describes what links like serial lines and classic ethernet must do to meet needs of this connectionless internet layer. It is not a layer in normal sense, but rather an interface between different hosts and transmission links. + +*** Internet layer +This layer roughly corresponds to the OSI network layer. Its job is to permit hosts to *inject packets into any network and have them travel independently to the destination*. That is, it will allow packets from go to one network to another while they are going to destination. This property that packets can travel through any network makes the whole network more robust, by allowing communication even if a few networks in some path are down. + +The packets may even arrive in a completely different order than in which they were sent. If in-order delivery was needed, then it's the responsibility of higher layers to rearrange them. + +It is similar to the *old mail system*. When a sequence of international letters are dropped in a mailbox from one country, most mails will be delivered to the correct address in destination country. The mails may not go in the same sequence they we entered in the mailbox. Furthermore, each country (network for our analogy) has it's own stamps, envelope size, and other rules and protocols. + +The internet layer defines an official packet format and protocol called *Internet Protocol* (IP), with a companion protocol *Internet Control Message Protocol* (ICMP) that helps its function. + +The job of internet layer is to deliver IP packets where they are supposed to go. Packet routing is the major obstacle for this layer. (IP has proven not effective to avoid congestion) +*** Transport layer +TODO : Here +*** Application layer +TODO : Here +@@html:
@@ +* Physical Layer +The lowest layer of the TCP/IP model. It describes the standard for the physical connections between nodes. It only views the data as a stream of bits and is concerned with transmission of bits without error. +@@html:
@@ +** Network Topology +The arrangement of nodes and links in a computer network is called network topology. There are various types of topologies + +TODO : Add images of network topology +*** Point-to-Point +It is the simplest connection with two nodes and a single link between them. +*** Daisy chaining +Also called linear topology, the nodes are connected in a series. Message will follows the whole chain till it reaches the destination. If a link fails in this topology, it is hard to find it, therefore it is not good for large networks. +*** Mesh topology +Every node is connected to another node through a direct link. In mesh topology, we use AHCP (Ad Hoc Configuration Protocol) and DHCP (Dynamic Host Configuration Protocol). + +So every node is connected to every other node directly connected through a link. So if there are $N$ nodes, then the each node has $(N - 1)$ links. + +If there are $N$ nodes, then in a mesh topology, the total number of links will be +\[ \text{number of links in mesh topology : } \frac{N(N - 1)}{2} \] +\[ \text{where } N \text{ is the number of nodes} \] + +Advantages of mesh topology are ++ communication is fast between any pair of nodes ++ data transfer is reliable since there are dedicated links between all nodes ++ since every pair of node has dedicated link, this provides security and privacy ++ the network is more robust single a failure of single device won't break the network + +Drawbacks of mesh topology are ++ installation is hard, since each link needs to be configured ++ excessive amount of cables and channels are required to connect all nodes ++ the cost of maintanance is very high + +Mesh topology is of two types +1. *Full mesh topology* : it is the traditional mesh topology, where every node is connected with other node thus every node has $(N - 1)$ connections. +2. *Partial mesh topology* : a more practical implementation where it is not necessary to connect all nodes to one another. Only channels with high amount of traffic are connected. This is not the traditional way to implement mesh topology. +*** Star topology +All nodes are connected to a central node (called the hub) through links. Hubs are not communication endpoints, their function is to forward packets across all ports. The hubs can either be passive in nature or active. + +Active hubs repeat and strengthen incoming transmissions, while passive hubs simply serve as a point of connectivity. Active hubs are also referred to as *repeaters*. + +Advantages of star topology are ++ only $N$ links are required to connect $N$ nodes to a network ++ each device will need only one port ++ if one link fails, it won't affect other links ++ cost effictive + +Drawback of star topology are ++ if the hub fails, then whole network is down ++ performace is dependant on the hubs + +*** Bus topology +In bus topology, all the nodes are connected to a single backbone line. This backbone is bi-directional, there are lines from node to backbone called droplines. This topology is not robust, since if backbone is damaged, the whole network is down. + +In this topology, various MAC protocols are followed like TDMA, Pure Aloha, CDMA, etc. + +Advantages of Bus topology ++ there is only a single link and $N$ drop lines required ++ reduces the cost for installation ++ CSMA is the most common method for this topology + +Drawbacks ++ if backbone fails, then whole network is down ++ this topology can't handle heavy traffic ++ adding new nodes slows the network ++ security is very lower +*** Ring topology +A daisy chain in a closed circular loop is called a ring topology. The most common method for communication in this topology is token passing. A *token* is a frame which is circulated around the network. The token is passed from one node to another till it reaches its destination. + +Advantages ++ better for traffic than bus topology ++ cheap to install and expand + +Disadvantages ++ single node failure can cause entire network to fail ++ troubleshooting is difficult ++ the topology is not secure + +*** Tree topology +Also called a star-bus topology. It can be seen as multiple star topologies connected via a backbone like in bus topology. Tree topology is hierarchical and there are parent and child star networks. It uses protocols like DHCP and SAC. The backbone is like the truck of the tree and various star networks branch out from it. + +The hubs connected directly to main backbone are central hubs. The hubs connected to other hubs are child hubs. This makes this topology very flexible. + +Advantages ++ allows networks to prioritize different computers ++ new nodes are easy to add ++ error detection and error correction is easy + +Drawbacks ++ if backbone fails the entire network is down. If a parent hub fails, then all the connected child network is down ++ cost of maintanance is high ++ reconfiguration is hard + +*** Hybrid topology +A hybrid topology is when a network combines two or more topologies in a way that does not resemble standard topologies. + +Advantages ++ this is very flexible ++ size of network can be easily expanded + +Disadvantages ++ it is challenging to design and maintain ++ the infrastructure needs specialization to maintain +@@html:
@@ +** Digital Modulation +Wires carry analog signals in form of continuously varying voltage. To send digital information, we need to devise analog signals to represent bits. This process of converting bits to analog signals is called digital modulation. +*** Bandwidth and maximum data rate +Bandwidth is the measure of carrying capacity of a medium. It is measured in Hertz (Hz). +\\ +Note : in many places, maximum data rate is also refered to as the bandwidth. +**** Maximum data rate for noiseless wires +Suppose our analog signal has $V$ different levels. Then for a noiseless wire with bandwidth of $B$, the maximum data rate is +\[ \text{maximum data rate} = 2\ B\ log_2V \text{ bits/sec} \] +When converting bits, if we are only using two levels, one for 0 bit and one for 1 bit. So, we can in most cases simplify the formula to +\[ \text{maximum data rate} = 2\ B \text{ bits/sec} \] +Example, a noiseless 3-kHz channel cannot transmit binary signal (two levels) at rate exceeding 6000 bps. +**** Maximum data rate for noisy wires +But the noiseless wire is an ideal case, which is not possible in real wires. Amount of noise is measured using ratio of signal power to noise power called *SNR (Signal-to-Noise Ratio)*. $S$ denotes signal power and $N$ denotes noise power. The ratio is expressed on a log scale as +\[ SNR = 10\ log_{10} (S/N) \text{ dB} \] +The unit of SNR is *decibals (dB)* + +Using SNR, the maximum data rate in a noisy medium is +\[ \text{maximum data rate} = B\ log_2(1 + S/N) \text{ bits/sec} \] +*Example*, suppose SNR is of 40 dB and bandwidth is 1MHz. Then, using formula for SNR, $S/N$ is $10^4$. Therefore, the maximum data rate is +\[ \text{maximum data rate} = 1\ log_2 (10^4) \text{ Mbps}\] +\[ \text{maximum data rate} = 13.288 \text{ Mbps} \] + +*** Non-Return to Zero (NRZ) +TODO : Add image +The most straightforward form of digital modulation is to use positive voltage for 1 bit and negative voltage to represent 0 bit. This scheme is called NRZ (Non-Return-to-Zero) + +The NRZ signal propogates down the wire. At the other end, the reciever converts in into bits by sampling the signal at regular intervals of time. This signal won't look the same as one that was sent. It will be distorted by noise. To decode bits receiver will map signal samples to closest symbols. + +The NRZ is simple, but is seldom used by itself in practice. We use more complex schemes to convert bits to signals. These schemes are called *line codes*. + +*** Increasing bandwidth efficiency by symbols +Bandwidth is a limited resource, therefore we must use it efficiently. + +With NRZ, the signal may cycle between positive and negative level every 2 bits (in case of alternating 1s and 0s). Therefore, atleast a bandwidth of B/2 Hz is needed when bit rate is B bits/sec. + +One strategy for using the limited bandwidth is to *increase the signaling levels*. For example, by using four voltages, we can send 2 bits at once as a single *symbol*. When the number of levels is 4, the maximum data rate increases. +\[ \text{maximum data rate} = 4\ B \text{ bits/sec} \] +So, by increasing the number of levels, we can send *symbols* rather than bits to increase performance. + +We call rate at which symbol changes the *symbol rate* to distinguish it from bit rate. An older name for symbol rate is *baud rate*. +\[ \text{bit rate} = \text{symbol rate } \times \text{ bits in a symbol} \] +Note: the number of levels does not need to be a power of 2. +*** Clock recovery +Clocks on both sender and reciever side are used to decode the incoming signal into bits. Suppose we are sending a signal with 20 consecutive zero's. In this case it will become hard to tell bits apart, 20 zero's will look very similar to 19 or 21 zero's. + +We could use accurate clocks for this problem. But when bit rate is in multiple Mbps, the clock would need to be accurate down to less than a fraction of microsecond. Therefore, accurate clocks are not a general solution. +**** Manchester encoding +TODO : Add image +A clever trick is to *mix a clock signal with the data signal* by XORing them together. This scheme is called *Manchester Encoding*. + +The clock should make a complete cycle for every bit that needs to be transmitted. Therefore, clock frequency needs to be twice of bit rate. In other words, if the bit rate is $B$ bits/sec, then the clock frequency will be $B$ Hz. + +Therefore, the downside of manchester encoding is that it requires *twice as much bandwidth as NRZ*. +**** Non-Return-to-Zero Inverted (NRZI) +TODO : Add image + +Clock recovery problems occur when there are long runs of 1's or 0's. NRZ-I encoding produces signals that are easy to translate with clock. + +In NRZI, rather than using levels to represent bits, *we use transitions to represent bits*. Example, 1 bit will be a transition and 0 bit will be no transition. A transition is going from one level to another. + +*USB uses NRZI* encoding to send bits. Long runs of 0 will still cause problems in NRZI. +**** 4B/5B +To truly fix the issue, we can break long runs of 0's by mapping group of bits to longer patterns that do not have consecutive 0's. + +A well known code of this type is 4B/5B. Every *4 bits are mapped to 5 bit patterns* which are then sent as analog signals. The translation table is fixed. + +The 5 bit patterns are designed in a way that *there are never more than three consecutive 0's*. This encoding will *add 25% overhead which is better than 100% overhead of manchester*. +| Data (4B) | Codeword (5B) | +|-----------+---------------| +| 0000 | 11110 | +| 0001 | 01001 | +| 0010 | 10100 | +| 0011 | 10101 | +| 0100 | 01010 | +| 0101 | 01011 | +| 0110 | 01110 | +| 0111 | 01111 | +| 1000 | 10010 | +| 1001 | 10011 | +| 1010 | 10110 | +| 1011 | 10111 | +| 1100 | 11010 | +| 1101 | 11011 | +| 1110 | 11100 | +| 1111 | 11101 | +Since there are 32 different 5 bit patterns and all of them are not used. We can have remaining patterns which don't have many successive 0's as control signals. + +4B/5B is usually used in conjunction to NRZI. +**** Scrambling +Another approach is scrambling the bits to reduce chances of long runs of 0's. + +A *scrambler* will XOR the data with a psuedorandom sequence before it is transmitted. This mixing will make data pseudorandom (assuming the sequence was chosen independent of data). The receiver can XOR the incoming bits with same sequence to recover the real data. Therefore, the pseudorandom sequence must be easy to create. It is usually given as seed to a simple random number generator. + +*Scrambling will add no overhead.* Also, repetitive data patterns can cause electromagnetic interference. Pseudorandom data is usually "white", i.e. has more uniform spread across signals *reducing interference.* + +However, since scrambling is pseudorandom there is *no guarentee that long runs won't occur.* With predictable data patterns, it is *easy for malicious users to send data that can cause long runs of 0's*, causing link to fail. Earlier standards had this defect, it was possible to send certain *"killer packets"* that caused problems. +*** Balanced signals +Signals that have almost as many positive voltages as negative voltages are called *balanced signals.* Balanced signals *average to zero*, which means they have no DC electrical component. + +We need balanced signals because ++ *Many mediums* such as coaxial cables or lines with transformers *strongly reduce the DC component of signals.* ++ A method of connecting receiver called *capacitive coupling* can only pass AC signals. ++ If signal is not balanced, we waste energy because DC component is filtered out. ++ Balanced signals have *easier clock recovery* since there is a mix of positive and negative voltages. ++ It helps in *calibration of receivers* as they can check average of signals to decode symbols. In *unbalanced signals, the average drifts causing errors.* + +@@html:
@@ diff --git a/main.tsk b/main.tsk new file mode 100644 index 0000000..3d6f3de --- /dev/null +++ b/main.tsk @@ -0,0 +1,7 @@ +* Export to HTML +#do +emacs --script src/export.el + +* Remove intermediate +#do +rm main.html~ diff --git a/src/export.el b/src/export.el new file mode 100644 index 0000000..65d5dfd --- /dev/null +++ b/src/export.el @@ -0,0 +1,9 @@ +;; In elisp, default-directory is the current directory +(add-to-list 'load-path "src") +;; If htmlize is outdated, just replace htmlize.el with the newer version lmao. +(require 'htmlize) + +(load-theme 'tsdh-light) + +(find-file "main.org") +(org-html-export-to-html) diff --git a/src/htmlize.el b/src/htmlize.el new file mode 100644 index 0000000..b158a65 --- /dev/null +++ b/src/htmlize.el @@ -0,0 +1,1864 @@ +;;; htmlize.el --- Convert buffer text and decorations to HTML. -*- lexical-binding: t -*- + +;; Copyright (C) 1997-2003,2005,2006,2009,2011,2012,2014,2017,2018,2020 Hrvoje Niksic + +;; Author: Hrvoje Niksic +;; Homepage: https://github.com/hniksic/emacs-htmlize +;; Keywords: hypermedia, extensions +;; Version: 1.57 + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; Boston, MA 02111-1307, USA. + +;;; Commentary: + +;; This package converts the buffer text and the associated +;; decorations to HTML. Mail to to discuss +;; features and additions. All suggestions are more than welcome. + +;; To use it, just switch to the buffer you want HTML-ized and type +;; `M-x htmlize-buffer'. You will be switched to a new buffer that +;; contains the resulting HTML code. You can edit and inspect this +;; buffer, or you can just save it with C-x C-w. `M-x htmlize-file' +;; will find a file, fontify it, and save the HTML version in +;; FILE.html, without any additional intervention. `M-x +;; htmlize-many-files' allows you to htmlize any number of files in +;; the same manner. `M-x htmlize-many-files-dired' does the same for +;; files marked in a dired buffer. + +;; htmlize supports three types of HTML output, selected by setting +;; `htmlize-output-type': `css', `inline-css', and `font'. In `css' +;; mode, htmlize uses cascading style sheets to specify colors; it +;; generates classes that correspond to Emacs faces and uses ... to color parts of text. In this mode, the +;; produced HTML is valid under the 4.01 strict DTD, as confirmed by +;; the W3C validator. `inline-css' is like `css', except the CSS is +;; put directly in the STYLE attribute of the SPAN element, making it +;; possible to paste the generated HTML into existing HTML documents. +;; In `font' mode, htmlize uses ... to +;; colorize HTML, which is not standard-compliant, but works better in +;; older browsers. `css' mode is the default. + +;; You can also use htmlize from your Emacs Lisp code. When called +;; non-interactively, `htmlize-buffer' and `htmlize-region' will +;; return the resulting HTML buffer, but will not change current +;; buffer or move the point. htmlize will do its best to work on +;; non-windowing Emacs sessions but the result will be limited to +;; colors supported by the terminal. + +;; htmlize aims for compatibility with older Emacs versions. Please +;; let me know if it doesn't work on the version of GNU Emacs that you +;; are using. The package relies on the presence of CL extensions; +;; please don't try to remove that dependency. I see no practical +;; problems with using the full power of the CL extensions, except +;; that one might learn to like them too much. + +;; The latest version is available at: +;; +;; +;; +;; + +;; Thanks go to the many people who have sent reports and contributed +;; comments, suggestions, and fixes. They include Ron Gut, Bob +;; Weiner, Toni Drabik, Peter Breton, Ville Skytta, Thomas Vogels, +;; Juri Linkov, Maciek Pasternacki, and many others. + +;; User quotes: "You sir, are a sick, sick, _sick_ person. :)" +;; -- Bill Perry, author of Emacs/W3 + + +;;; Code: + +(require 'cl-lib) +(eval-when-compile + (defvar font-lock-auto-fontify) + (defvar font-lock-support-mode) + (defvar global-font-lock-mode)) + +(defconst htmlize-version "1.57") + +(defgroup htmlize nil + "Convert buffer text and faces to HTML." + :group 'hypermedia) + +(defcustom htmlize-head-tags "" + "Additional tags to insert within HEAD of the generated document." + :type 'string + :group 'htmlize) + +(defcustom htmlize-output-type 'css + "Output type of generated HTML, one of `css', `inline-css', or `font'. +When set to `css' (the default), htmlize will generate a style sheet +with description of faces, and use it in the HTML document, specifying +the faces in the actual text with . + +When set to `inline-css', the style will be generated as above, but +placed directly in the STYLE attribute of the span ELEMENT: . This makes it easier to paste the resulting HTML to +other documents. + +When set to `font', the properties will be set using layout tags +, , , , and . + +`css' output is normally preferred, but `font' is still useful for +supporting old, pre-CSS browsers, and both `inline-css' and `font' for +easier embedding of colorized text in foreign HTML documents (no style +sheet to carry around)." + :type '(choice (const css) (const inline-css) (const font)) + :group 'htmlize) + +(defcustom htmlize-use-images t + "Whether htmlize generates `img' for images attached to buffer contents." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-force-inline-images nil + "Non-nil means generate all images inline using data URLs. +Normally htmlize converts image descriptors with :file properties to +relative URIs, and those with :data properties to data URIs. With this +flag set, the images specified as a file name are loaded into memory and +embedded in the HTML as data URIs." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-max-alt-text 100 + "Maximum size of text to use as ALT text in images. + +Normally when htmlize encounters text covered by the `display' property +that specifies an image, it generates an `alt' attribute containing the +original text. If the text is larger than `htmlize-max-alt-text' characters, +this will not be done." + :type 'integer + :group 'htmlize) + +(defcustom htmlize-transform-image 'htmlize-default-transform-image + "Function called to modify the image descriptor. + +The function is called with the image descriptor found in the buffer and +the text the image is supposed to replace. It should return a (possibly +different) image descriptor property list or a replacement string to use +instead of of the original buffer text. + +Returning nil is the same as returning the original text." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-generate-hyperlinks t + "Non-nil means auto-generate the links from URLs and mail addresses in buffer. + +This is on by default; set it to nil if you don't want htmlize to +autogenerate such links. Note that this option only turns off automatic +search for contents that looks like URLs and converting them to links. +It has no effect on whether htmlize respects the `htmlize-link' property." + :type 'boolean + :group 'htmlize) + +(defcustom htmlize-hyperlink-style " + a { + color: inherit; + background-color: inherit; + font: inherit; + text-decoration: inherit; + } + a:hover { + text-decoration: underline; + } +" + "The CSS style used for hyperlinks when in CSS mode." + :type 'string + :group 'htmlize) + +(defcustom htmlize-replace-form-feeds t + "Non-nil means replace form feeds in source code with HTML separators. +Form feeds are the ^L characters at line beginnings that are sometimes +used to separate sections of source code. If this variable is set to +`t', form feed characters are replaced with the
separator. If this +is a string, it specifies the replacement to use. Note that
 is
+temporarily closed before the separator is inserted, so the default
+replacement is effectively \"

\".  If you specify
+another replacement, don't forget to close and reopen the 
 if you
+want the output to remain valid HTML.
+
+If you need more elaborate processing, set this to nil and use
+htmlize-after-hook."
+  :type 'boolean
+  :group 'htmlize)
+
+(defcustom htmlize-html-charset nil
+  "The charset declared by the resulting HTML documents.
+When non-nil, causes htmlize to insert the following in the HEAD section
+of the generated HTML:
+
+  
+
+where CHARSET is the value you've set for htmlize-html-charset.  Valid
+charsets are defined by MIME and include strings like \"iso-8859-1\",
+\"iso-8859-15\", \"utf-8\", etc.
+
+If you are using non-Latin-1 charsets, you might need to set this for
+your documents to render correctly.  Also, the W3C validator requires
+submitted HTML documents to declare a charset.  So if you care about
+validation, you can use this to prevent the validator from bitching.
+
+Needless to say, if you set this, you should actually make sure that
+the buffer is in the encoding you're claiming it is in.  (This is
+normally achieved by using the correct file coding system for the
+buffer.)  If you don't understand what that means, you should probably
+leave this option in its default setting."
+  :type '(choice (const :tag "Unset" nil)
+		 string)
+  :group 'htmlize)
+
+(defcustom htmlize-convert-nonascii-to-entities t
+  "Whether non-ASCII characters should be converted to HTML entities.
+
+When this is non-nil, characters with codes in the 128-255 range will be
+considered Latin 1 and rewritten as \"&#CODE;\".  Characters with codes
+above 255 will be converted to \"&#UCS;\", where UCS denotes the Unicode
+code point of the character.  If the code point cannot be determined,
+the character will be copied unchanged, as would be the case if the
+option were nil.
+
+When the option is nil, the non-ASCII characters are copied to HTML
+without modification.  In that case, the web server and/or the browser
+must be set to understand the encoding that was used when saving the
+buffer.  (You might also want to specify it by setting
+`htmlize-html-charset'.)
+
+Note that in an HTML entity \"&#CODE;\", CODE is always a UCS code point,
+which has nothing to do with the charset the page is in.  For example,
+\"©\" *always* refers to the copyright symbol, regardless of charset
+specified by the META tag or the charset sent by the HTTP server.  In
+other words, \"©\" is exactly equivalent to \"©\".
+
+For most people htmlize will work fine with this option left at the
+default setting; don't change it unless you know what you're doing."
+  :type 'sexp
+  :group 'htmlize)
+
+(defcustom htmlize-ignore-face-size 'absolute
+  "Whether face size should be ignored when generating HTML.
+If this is nil, face sizes are used.  If set to t, sizes are ignored
+If set to `absolute', only absolute size specifications are ignored.
+Please note that font sizes only work with CSS-based output types."
+  :type '(choice (const :tag "Don't ignore" nil)
+		 (const :tag "Ignore all" t)
+		 (const :tag "Ignore absolute" absolute))
+  :group 'htmlize)
+
+(defcustom htmlize-css-name-prefix ""
+  "The prefix used for CSS names.
+The CSS names that htmlize generates from face names are often too
+generic for CSS files; for example, `font-lock-type-face' is transformed
+to `type'.  Use this variable to add a prefix to the generated names.
+The string \"htmlize-\" is an example of a reasonable prefix."
+  :type 'string
+  :group 'htmlize)
+
+(defcustom htmlize-use-rgb-txt t
+  "Whether `rgb.txt' should be used to convert color names to RGB.
+
+This conversion means determining, for instance, that the color
+\"IndianRed\" corresponds to the (205, 92, 92) RGB triple.  `rgb.txt'
+is the X color database that maps hundreds of color names to such RGB
+triples.  When this variable is non-nil, `htmlize' uses `rgb.txt' to
+look up color names.
+
+If this variable is nil, htmlize queries Emacs for RGB components of
+colors using `color-instance-rgb-components' and `color-values'.
+This can yield incorrect results on non-true-color displays.
+
+If the `rgb.txt' file is not found (which will be the case if you're
+running Emacs on non-X11 systems), this option is ignored."
+  :type 'boolean
+  :group 'htmlize)
+
+(defvar htmlize-face-overrides nil
+  "Overrides for face definitions.
+
+Normally face definitions are taken from Emacs settings for fonts
+in the current frame.  For faces present in this plist, the
+definitions will be used instead.  Keys in the plist are symbols
+naming the face and values are the overriding definitions.  For
+example:
+
+  (setq htmlize-face-overrides
+        '(font-lock-warning-face \"black\"
+          font-lock-function-name-face \"red\"
+          font-lock-comment-face \"blue\"
+          default (:foreground \"dark-green\" :background \"yellow\")))
+
+This variable can be also be `let' bound when running `htmlize-buffer'.")
+
+(defcustom htmlize-untabify t
+  "Non-nil means untabify buffer contents during htmlization."
+  :type 'boolean
+  :group 'htmlize)
+
+(defcustom htmlize-html-major-mode nil
+  "The mode the newly created HTML buffer will be put in.
+Set this to nil if you prefer the default (fundamental) mode."
+  :type '(radio (const :tag "No mode (fundamental)" nil)
+		 (function-item html-mode)
+		 (function :tag "User-defined major mode"))
+  :group 'htmlize)
+
+(defcustom htmlize-pre-style nil
+  "When non-nil, `
' tags will be decorated with style
+information in `font' and `inline-css' modes. This allows a
+consistent background for captures of regions."
+  :type 'boolean
+  :group 'htmlize)
+
+(defvar htmlize-before-hook nil
+  "Hook run before htmlizing a buffer.
+The hook functions are run in the source buffer (not the resulting HTML
+buffer).")
+
+(defvar htmlize-after-hook nil
+  "Hook run after htmlizing a buffer.
+Unlike `htmlize-before-hook', these functions are run in the generated
+HTML buffer.  You may use them to modify the outlook of the final HTML
+output.")
+
+(defvar htmlize-file-hook nil
+  "Hook run by `htmlize-file' after htmlizing a file, but before saving it.")
+
+(defvar htmlize-buffer-places)
+
+;;; Some cross-Emacs compatibility.
+
+;; We need a function that efficiently finds the next change of a
+;; property regardless of whether the change occurred because of a
+;; text property or an extent/overlay.
+(defun htmlize-next-change (pos prop &optional limit)
+  (if prop
+      (next-single-char-property-change pos prop nil limit)
+    (next-char-property-change pos limit)))
+
+(defun htmlize-overlay-faces-at (pos)
+  (delq nil (mapcar (lambda (o) (overlay-get o 'face)) (overlays-at pos))))
+
+(defun htmlize-next-face-change (pos &optional limit)
+  ;; (htmlize-next-change pos 'face limit) would skip over entire
+  ;; overlays that specify the `face' property, even when they
+  ;; contain smaller text properties that also specify `face'.
+  ;; Emacs display engine merges those faces, and so must we.
+  (or limit
+      (setq limit (point-max)))
+  (let ((next-prop (next-single-property-change pos 'face nil limit))
+        (overlay-faces (htmlize-overlay-faces-at pos)))
+    (while (progn
+             (setq pos (next-overlay-change pos))
+             (and (< pos next-prop)
+                  (equal overlay-faces (htmlize-overlay-faces-at pos)))))
+    (setq pos (min pos next-prop))
+    ;; Additionally, we include the entire region that specifies the
+    ;; `display' property.
+    (when (get-char-property pos 'display)
+      (setq pos (next-single-char-property-change pos 'display nil limit)))
+    pos))
+
+(defmacro htmlize-lexlet (&rest letforms)
+  (declare (indent 1) (debug let))
+  (if (and (boundp 'lexical-binding)
+           lexical-binding)
+      `(let ,@letforms)
+    ;; cl extensions have a macro implementing lexical let
+    `(lexical-let ,@letforms)))
+
+
+;;; Transformation of buffer text: HTML escapes, untabification, etc.
+
+(defvar htmlize-basic-character-table
+  ;; Map characters in the 0-127 range to either one-character strings
+  ;; or to numeric entities.
+  (let ((table (make-vector 128 ?\0)))
+    ;; Map characters in the 32-126 range to themselves, others to
+    ;; &#CODE entities;
+    (dotimes (i 128)
+      (setf (aref table i) (if (and (>= i 32) (<= i 126))
+			       (char-to-string i)
+			     (format "&#%d;" i))))
+    ;; Set exceptions manually.
+    (setf
+     ;; Don't escape newline, carriage return, and TAB.
+     (aref table ?\n) "\n"
+     (aref table ?\r) "\r"
+     (aref table ?\t) "\t"
+     ;; Escape &, <, and >.
+     (aref table ?&) "&"
+     (aref table ?<) "<"
+     (aref table ?>) ">"
+     ;; Not escaping '"' buys us a measurable speedup.  It's only
+     ;; necessary to quote it for strings used in attribute values,
+     ;; which htmlize doesn't typically do.
+     ;(aref table ?\") """
+     )
+    table))
+
+;; A cache of HTML representation of non-ASCII characters.  Depending
+;; on the setting of `htmlize-convert-nonascii-to-entities', this maps
+;; non-ASCII characters to either "&#;" or "" (mapconcat's
+;; mapper must always return strings).  It's only filled as characters
+;; are encountered, so that in a buffer with e.g. French text, it will
+;; only ever contain French accented characters as keys.  It's cleared
+;; on each entry to htmlize-buffer-1 to allow modifications of
+;; `htmlize-convert-nonascii-to-entities' to take effect.
+(defvar htmlize-extended-character-cache (make-hash-table :test 'eq))
+
+(defun htmlize-protect-string (string)
+  "HTML-protect string, escaping HTML metacharacters and I18N chars."
+  ;; Only protecting strings that actually contain unsafe or non-ASCII
+  ;; chars removes a lot of unnecessary funcalls and consing.
+  (if (not (string-match "[^\r\n\t -%'-;=?-~]" string))
+      string
+    (mapconcat (lambda (char)
+		 (cond
+		  ((< char 128)
+		   ;; ASCII: use htmlize-basic-character-table.
+		   (aref htmlize-basic-character-table char))
+		  ((gethash char htmlize-extended-character-cache)
+		   ;; We've already seen this char; return the cached
+		   ;; string.
+		   )
+		  ((not htmlize-convert-nonascii-to-entities)
+		   ;; If conversion to entities is not desired, always
+		   ;; copy the char literally.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (char-to-string char)))
+		  ((< char 256)
+		   ;; Latin 1: no need to call encode-char.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (format "&#%d;" char)))
+		  ((encode-char char 'ucs)
+                   ;; Must check if encode-char works for CHAR;
+                   ;; it fails for Arabic and possibly elsewhere.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (format "&#%d;" (encode-char char 'ucs))))
+		  (t
+		   ;; encode-char doesn't work for this char.  Copy it
+		   ;; unchanged and hope for the best.
+		   (setf (gethash char htmlize-extended-character-cache)
+			 (char-to-string char)))))
+	       string "")))
+
+(defun htmlize-attr-escape (string)
+  ;; Like htmlize-protect-string, but also escapes double-quoted
+  ;; strings to make it usable in attribute values.
+  (setq string (htmlize-protect-string string))
+  (if (not (string-match "\"" string))
+      string
+    (mapconcat (lambda (char)
+                 (if (eql char ?\")
+                     """
+                   (char-to-string char)))
+               string "")))
+
+(defsubst htmlize-concat (list)
+  (if (and (consp list) (null (cdr list)))
+      ;; Don't create a new string in the common case where the list only
+      ;; consists of one element.
+      (car list)
+    (apply #'concat list)))
+
+(defun htmlize-format-link (linkprops text)
+  (let ((uri (if (stringp linkprops)
+                 linkprops
+               (plist-get linkprops :uri)))
+        (escaped-text (htmlize-protect-string text)))
+    (if uri
+        (format "%s" (htmlize-attr-escape uri) escaped-text)
+      escaped-text)))
+
+(defun htmlize-escape-or-link (string)
+  ;; Escape STRING and/or add hyperlinks.  STRING comes from a
+  ;; `display' property.
+  (let ((pos 0) (end (length string)) outlist)
+    (while (< pos end)
+      (let* ((link (get-char-property pos 'htmlize-link string))
+             (next-link-change (next-single-property-change
+                                pos 'htmlize-link string end))
+             (chunk (substring string pos next-link-change)))
+        (push
+         (cond (link
+                (htmlize-format-link link chunk))
+               ((get-char-property 0 'htmlize-literal chunk)
+                chunk)
+               (t
+                (htmlize-protect-string chunk)))
+         outlist)
+        (setq pos next-link-change)))
+    (htmlize-concat (nreverse outlist))))
+
+(defun htmlize-display-prop-to-html (display text)
+  (let (desc)
+    (cond ((stringp display)
+           ;; Emacs ignores recursive display properties.
+           (htmlize-escape-or-link display))
+          ((not (eq (car-safe display) 'image))
+           (htmlize-protect-string text))
+          ((null (setq desc (funcall htmlize-transform-image
+                                     (cdr display) text)))
+           (htmlize-escape-or-link text))
+          ((stringp desc)
+           (htmlize-escape-or-link desc))
+          (t
+           (htmlize-generate-image desc text)))))
+
+(defun htmlize-string-to-html (string)
+  ;; Convert the string to HTML, including images attached as
+  ;; `display' property and links as `htmlize-link' property.  In a
+  ;; string without images or links, this is equivalent to
+  ;; `htmlize-protect-string'.
+  (let ((pos 0) (end (length string)) outlist)
+    (while (< pos end)
+      (let* ((display (get-char-property pos 'display string))
+             (next-display-change (next-single-property-change
+                                   pos 'display string end))
+             (chunk (substring string pos next-display-change)))
+        (push
+         (if display
+             (htmlize-display-prop-to-html display chunk)
+           (htmlize-escape-or-link chunk))
+         outlist)
+        (setq pos next-display-change)))
+    (htmlize-concat (nreverse outlist))))
+
+(defun htmlize-default-transform-image (imgprops _text)
+  "Default transformation of image descriptor to something usable in HTML.
+
+If `htmlize-use-images' is nil, the function always returns nil, meaning
+use original text.  Otherwise, it tries to find the image for images that
+specify a file name.  If `htmlize-force-inline-images' is non-nil, it also
+converts the :file attribute to :data and returns the modified property
+list."
+  (when htmlize-use-images
+    (when (plist-get imgprops :file)
+      (let ((location (plist-get (cdr (find-image (list imgprops))) :file)))
+        (when location
+          (setq imgprops (plist-put (cl-copy-list imgprops) :file location)))))
+    (if htmlize-force-inline-images
+        (let ((location (plist-get imgprops :file))
+              data)
+          (when location
+            (with-temp-buffer
+              (condition-case nil
+                  (progn
+                    (insert-file-contents-literally location)
+                    (setq data (buffer-string)))
+                (error nil))))
+          ;; if successful, return the new plist, otherwise return
+          ;; nil, which will use the original text
+          (and data
+               (plist-put (plist-put imgprops :file nil)
+                          :data data)))
+      imgprops)))
+
+(defun htmlize-alt-text (_imgprops origtext)
+  (and (/= (length origtext) 0)
+       (<= (length origtext) htmlize-max-alt-text)
+       (not (string-match "[\0-\x1f]" origtext))
+       origtext))
+
+(defun htmlize-generate-image (imgprops origtext)
+  (let* ((alt-text (htmlize-alt-text imgprops origtext))
+         (alt-attr (if alt-text
+                       (format " alt=\"%s\"" (htmlize-attr-escape alt-text))
+                     "")))
+    (cond ((plist-get imgprops :file)
+           ;; Try to find the image in image-load-path
+           (let* ((found-props (cdr (find-image (list imgprops))))
+                  (file (or (plist-get found-props :file)
+                            (plist-get imgprops :file))))
+             (format ""
+                     (htmlize-attr-escape (file-relative-name file))
+                     alt-attr)))
+          ((plist-get imgprops :data)
+           (format ""
+                   (or (plist-get imgprops :type) "")
+                   (base64-encode-string (plist-get imgprops :data))
+                   alt-attr)))))
+
+(defconst htmlize-ellipsis "...")
+(put-text-property 0 (length htmlize-ellipsis) 'htmlize-ellipsis t htmlize-ellipsis)
+
+(defun htmlize-match-inv-spec (inv)
+  (cl-member inv buffer-invisibility-spec
+             :key (lambda (i)
+                    (if (symbolp i) i (car i)))))
+
+(defun htmlize-decode-invisibility-spec (invisible)
+  ;; Return t, nil, or `ellipsis', depending on how invisible text should be inserted.
+
+  (if (not (listp buffer-invisibility-spec))
+      ;; If buffer-invisibility-spec is not a list, then all
+      ;; characters with non-nil `invisible' property are visible.
+      (not invisible)
+
+    ;; Otherwise, the value of a non-nil `invisible' property can be:
+    ;; 1. a symbol -- make the text invisible if it matches
+    ;;    buffer-invisibility-spec.
+    ;; 2. a list of symbols -- make the text invisible if
+    ;;    any symbol in the list matches
+    ;;    buffer-invisibility-spec.
+    ;; If the match of buffer-invisibility-spec has a non-nil
+    ;; CDR, replace the invisible text with an ellipsis.
+    (let ((match (if (symbolp invisible)
+                     (htmlize-match-inv-spec invisible)
+                   (cl-some #'htmlize-match-inv-spec invisible))))
+      (cond ((null match) t)
+            ((cdr-safe (car match)) 'ellipsis)
+            (t nil)))))
+
+(defun htmlize-add-before-after-strings (beg end text)
+  ;; Find overlays specifying before-string and after-string in [beg,
+  ;; pos).  If any are found, splice them into TEXT and return the new
+  ;; text.
+  (let (additions)
+    (dolist (overlay (overlays-in beg end))
+      (let ((before (overlay-get overlay 'before-string))
+            (after (overlay-get overlay 'after-string)))
+        (when after
+          (push (cons (- (overlay-end overlay) beg)
+                      after)
+                additions))
+        (when before
+          (push (cons (- (overlay-start overlay) beg)
+                      before)
+                additions))))
+    (if additions
+        (let ((textlist nil)
+              (strpos 0))
+          (dolist (add (cl-stable-sort additions #'< :key #'car))
+            (let ((addpos (car add))
+                  (addtext (cdr add)))
+              (push (substring text strpos addpos) textlist)
+              (push addtext textlist)
+              (setq strpos addpos)))
+          (push (substring text strpos) textlist)
+          (apply #'concat (nreverse textlist)))
+      text)))
+
+(defun htmlize-copy-prop (prop beg end string)
+  ;; Copy the specified property from the specified region of the
+  ;; buffer to the target string.  We cannot rely on Emacs to copy the
+  ;; property because we want to handle properties coming from both
+  ;; text properties and overlays.
+  (let ((pos beg))
+    (while (< pos end)
+      (let ((value (get-char-property pos prop))
+            (next-change (htmlize-next-change pos prop end)))
+        (when value
+          (put-text-property (- pos beg) (- next-change beg)
+                             prop value string))
+        (setq pos next-change)))))
+
+(defun htmlize-get-text-with-display (beg end)
+  ;; Like buffer-substring-no-properties, except it copies the
+  ;; `display' property from the buffer, if found.
+  (let ((text (buffer-substring-no-properties beg end)))
+    (htmlize-copy-prop 'display beg end text)
+    (htmlize-copy-prop 'htmlize-link beg end text)
+    (setq text (htmlize-add-before-after-strings beg end text))
+    text))
+
+(defun htmlize-buffer-substring-no-invisible (beg end)
+  ;; Like buffer-substring-no-properties, but don't copy invisible
+  ;; parts of the region.  Where buffer-substring-no-properties
+  ;; mandates an ellipsis to be shown, htmlize-ellipsis is inserted.
+  (let ((pos beg)
+	visible-list invisible show last-show next-change)
+    ;; Iterate over the changes in the `invisible' property and filter
+    ;; out the portions where it's non-nil, i.e. where the text is
+    ;; invisible.
+    (while (< pos end)
+      (setq invisible (get-char-property pos 'invisible)
+	    next-change (htmlize-next-change pos 'invisible end)
+            show (htmlize-decode-invisibility-spec invisible))
+      (cond ((eq show t)
+	     (push (htmlize-get-text-with-display pos next-change)
+                   visible-list))
+            ((and (eq show 'ellipsis)
+                  (not (eq last-show 'ellipsis))
+                  ;; Conflate successive ellipses.
+                  (push htmlize-ellipsis visible-list))))
+      (setq pos next-change last-show show))
+    (htmlize-concat (nreverse visible-list))))
+
+(defun htmlize-trim-ellipsis (text)
+  ;; Remove htmlize-ellipses ("...") from the beginning of TEXT if it
+  ;; starts with it.  It checks for the special property of the
+  ;; ellipsis so it doesn't work on ordinary text that begins with
+  ;; "...".
+  (if (get-text-property 0 'htmlize-ellipsis text)
+      (substring text (length htmlize-ellipsis))
+    text))
+
+(defconst htmlize-tab-spaces
+  ;; A table of strings with spaces.  (aref htmlize-tab-spaces 5) is
+  ;; like (make-string 5 ?\ ), except it doesn't cons.
+  (let ((v (make-vector 32 nil)))
+    (dotimes (i (length v))
+      (setf (aref v i) (make-string i ?\ )))
+    v))
+
+(defun htmlize-untabify-string (text start-column)
+  "Untabify TEXT, assuming it starts at START-COLUMN."
+  (let ((column start-column)
+	(last-match 0)
+	(chunk-start 0)
+	chunks match-pos tab-size)
+    (while (string-match "[\t\n]" text last-match)
+      (setq match-pos (match-beginning 0))
+      (cond ((eq (aref text match-pos) ?\t)
+	     ;; Encountered a tab: create a chunk of text followed by
+	     ;; the expanded tab.
+	     (push (substring text chunk-start match-pos) chunks)
+	     ;; Increase COLUMN by the length of the text we've
+	     ;; skipped since last tab or newline.  (Encountering
+	     ;; newline resets it.)
+	     (cl-incf column (- match-pos last-match))
+	     ;; Calculate tab size based on tab-width and COLUMN.
+	     (setq tab-size (- tab-width (% column tab-width)))
+	     ;; Expand the tab, carefully recreating the `display'
+	     ;; property if one was on the TAB.
+             (let ((display (get-text-property match-pos 'display text))
+                   (expanded-tab (aref htmlize-tab-spaces tab-size)))
+               (when display
+                 (put-text-property 0 tab-size 'display display expanded-tab))
+               (push expanded-tab chunks))
+	     (cl-incf column tab-size)
+	     (setq chunk-start (1+ match-pos)))
+	    (t
+	     ;; Reset COLUMN at beginning of line.
+	     (setq column 0)))
+      (setq last-match (1+ match-pos)))
+    ;; If no chunks have been allocated, it means there have been no
+    ;; tabs to expand.  Return TEXT unmodified.
+    (if (null chunks)
+	text
+      (when (< chunk-start (length text))
+	;; Push the remaining chunk.
+	(push (substring text chunk-start) chunks))
+      ;; Generate the output from the available chunks.
+      (htmlize-concat (nreverse chunks)))))
+
+(defun htmlize-extract-text (beg end trailing-ellipsis)
+  ;; Extract buffer text, sans the invisible parts.  Then
+  ;; untabify it and escape the HTML metacharacters.
+  (let ((text (htmlize-buffer-substring-no-invisible beg end)))
+    (when trailing-ellipsis
+      (setq text (htmlize-trim-ellipsis text)))
+    ;; If TEXT ends up empty, don't change trailing-ellipsis.
+    (when (> (length text) 0)
+      (setq trailing-ellipsis
+            (get-text-property (1- (length text))
+                               'htmlize-ellipsis text)))
+    (when htmlize-untabify
+      (setq text (htmlize-untabify-string text (current-column))))
+    (setq text (htmlize-string-to-html text))
+    (cl-values text trailing-ellipsis)))
+
+(defun htmlize-despam-address (string)
+  "Replace every occurrence of '@' in STRING with %40.
+This is used to protect mailto links without modifying their meaning."
+  ;; Suggested by Ville Skytta.
+  (while (string-match "@" string)
+    (setq string (replace-match "%40" nil t string)))
+  string)
+
+(defun htmlize-make-tmp-overlay (beg end props)
+  (let ((overlay (make-overlay beg end)))
+    (overlay-put overlay 'htmlize-tmp-overlay t)
+    (while props
+      (overlay-put overlay (pop props) (pop props)))
+    overlay))
+
+(defun htmlize-delete-tmp-overlays ()
+  (dolist (overlay (overlays-in (point-min) (point-max)))
+    (when (overlay-get overlay 'htmlize-tmp-overlay)
+      (delete-overlay overlay))))
+
+(defun htmlize-make-link-overlay (beg end uri)
+  (htmlize-make-tmp-overlay beg end `(htmlize-link (:uri ,uri))))
+
+(defun htmlize-create-auto-links ()
+  "Add `htmlize-link' property to all mailto links in the buffer."
+  (save-excursion
+    (goto-char (point-min))
+    (while (re-search-forward
+            "<\\(\\(mailto:\\)?\\([-=+_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+\\)\\)>"
+            nil t)
+      (let* ((address (match-string 3))
+             (beg (match-beginning 0)) (end (match-end 0))
+             (uri (concat "mailto:" (htmlize-despam-address address))))
+        (htmlize-make-link-overlay beg end uri)))
+    (goto-char (point-min))
+    (while (re-search-forward "<\\(\\(URL:\\)?\\([a-zA-Z]+://[^;]+\\)\\)>"
+                              nil t)
+      (htmlize-make-link-overlay
+       (match-beginning 0) (match-end 0) (match-string 3)))))
+
+;; Tests for htmlize-create-auto-links:
+
+;; 
+;; 
+;; 
+;; 
+;; 
+;; 
+
+(defun htmlize-shadow-form-feeds ()
+  (let ((s "\n
")) + (put-text-property 0 (length s) 'htmlize-literal t s) + (let ((disp `(display ,s))) + (while (re-search-forward "\n\^L" nil t) + (let* ((beg (match-beginning 0)) + (end (match-end 0)) + (form-feed-pos (1+ beg)) + ;; don't process ^L if invisible or covered by `display' + (show (and (htmlize-decode-invisibility-spec + (get-char-property form-feed-pos 'invisible)) + (not (get-char-property form-feed-pos 'display))))) + (when show + (htmlize-make-tmp-overlay beg end disp))))))) + +(defun htmlize-defang-local-variables () + ;; Juri Linkov reports that an HTML-ized "Local variables" can lead + ;; visiting the HTML to fail with "Local variables list is not + ;; properly terminated". He suggested changing the phrase to + ;; syntactically equivalent HTML that Emacs doesn't recognize. + (goto-char (point-min)) + (while (search-forward "Local Variables:" nil t) + (replace-match "Local Variables:" nil t))) + + +;;; Color handling. + +(defvar htmlize-x-library-search-path + `(,data-directory + "/etc/X11/rgb.txt" + "/usr/share/X11/rgb.txt" + ;; the remainder of this list really belongs in a museum + "/usr/X11R6/lib/X11/" + "/usr/X11R5/lib/X11/" + "/usr/lib/X11R6/X11/" + "/usr/lib/X11R5/X11/" + "/usr/local/X11R6/lib/X11/" + "/usr/local/X11R5/lib/X11/" + "/usr/local/lib/X11R6/X11/" + "/usr/local/lib/X11R5/X11/" + "/usr/X11/lib/X11/" + "/usr/lib/X11/" + "/usr/local/lib/X11/" + "/usr/X386/lib/X11/" + "/usr/x386/lib/X11/" + "/usr/XFree86/lib/X11/" + "/usr/unsupported/lib/X11/" + "/usr/athena/lib/X11/" + "/usr/local/x11r5/lib/X11/" + "/usr/lpp/Xamples/lib/X11/" + "/usr/openwin/lib/X11/" + "/usr/openwin/share/lib/X11/")) + +(defun htmlize-get-color-rgb-hash (&optional rgb-file) + "Return a hash table mapping X color names to RGB values. +The keys in the hash table are X11 color names, and the values are the +#rrggbb RGB specifications, extracted from `rgb.txt'. + +If RGB-FILE is nil, the function will try hard to find a suitable file +in the system directories. + +If no rgb.txt file is found, return nil." + (let ((rgb-file (or rgb-file (locate-file + "rgb.txt" + htmlize-x-library-search-path))) + (hash nil)) + (when rgb-file + (with-temp-buffer + (insert-file-contents rgb-file) + (setq hash (make-hash-table :test 'equal)) + (while (not (eobp)) + (cond ((looking-at "^\\s-*\\([!#]\\|$\\)") + ;; Skip comments and empty lines. + ) + ((looking-at + "[ \t]*\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\(.*\\)") + (setf (gethash (downcase (match-string 4)) hash) + (format "#%02x%02x%02x" + (string-to-number (match-string 1)) + (string-to-number (match-string 2)) + (string-to-number (match-string 3))))) + (t + (error + "Unrecognized line in %s: %s" + rgb-file + (buffer-substring (point) (progn (end-of-line) (point)))))) + (forward-line 1)))) + hash)) + +;; Compile the RGB map when loaded. On systems where rgb.txt is +;; missing, the value of the variable will be nil, and rgb.txt will +;; not be used. +(defvar htmlize-color-rgb-hash (htmlize-get-color-rgb-hash)) + +;;; Face handling. + +(defun htmlize-face-color-internal (face fg) + ;; Used only under GNU Emacs. Return the color of FACE, but don't + ;; return "unspecified-fg" or "unspecified-bg". If the face is + ;; `default' and the color is unspecified, look up the color in + ;; frame parameters. + (let* ((function (if fg #'face-foreground #'face-background)) + (color (funcall function face nil t))) + (when (and (eq face 'default) (null color)) + (setq color (cdr (assq (if fg 'foreground-color 'background-color) + (frame-parameters))))) + (when (or (eq color 'unspecified) + (equal color "unspecified-fg") + (equal color "unspecified-bg")) + (setq color nil)) + (when (and (eq face 'default) + (null color)) + ;; Assuming black on white doesn't seem right, but I can't think + ;; of anything better to do. + (setq color (if fg "black" "white"))) + color)) + +(defun htmlize-face-foreground (face) + ;; Return the name of the foreground color of FACE. If FACE does + ;; not specify a foreground color, return nil. + (htmlize-face-color-internal face t)) + +(defun htmlize-face-background (face) + ;; Return the name of the background color of FACE. If FACE does + ;; not specify a background color, return nil. + ;; GNU Emacs. + (htmlize-face-color-internal face nil)) + +;; Convert COLOR to the #RRGGBB string. If COLOR is already in that +;; format, it's left unchanged. + +(defun htmlize-color-to-rgb (color) + (let ((rgb-string nil)) + (cond ((null color) + ;; Ignore nil COLOR because it means that the face is not + ;; specifying any color. Hence (htmlize-color-to-rgb nil) + ;; returns nil. + ) + ((string-match "\\`#" color) + ;; The color is already in #rrggbb format. + (setq rgb-string color)) + ((and htmlize-use-rgb-txt + htmlize-color-rgb-hash) + ;; Use of rgb.txt is requested, and it's available on the + ;; system. Use it. + (setq rgb-string (gethash (downcase color) htmlize-color-rgb-hash))) + (t + ;; We're getting the RGB components from Emacs. + (let ((rgb (mapcar (lambda (arg) + (/ arg 256)) + (color-values color)))) + (when rgb + (setq rgb-string (apply #'format "#%02x%02x%02x" rgb)))))) + ;; If RGB-STRING is still nil, it means the color cannot be found, + ;; for whatever reason. In that case just punt and return COLOR. + ;; Most browsers support a decent set of color names anyway. + (or rgb-string color))) + +;; We store the face properties we care about into an +;; `htmlize-fstruct' type. That way we only have to analyze face +;; properties, which can be time consuming, once per each face. The +;; mapping between Emacs faces and htmlize-fstructs is established by +;; htmlize-make-face-map. The name "fstruct" refers to variables of +;; type `htmlize-fstruct', while the term "face" is reserved for Emacs +;; faces. + +(cl-defstruct htmlize-fstruct + foreground ; foreground color, #rrggbb + background ; background color, #rrggbb + size ; size + boldp ; whether face is bold + italicp ; whether face is italic + underlinep ; whether face is underlined + overlinep ; whether face is overlined + strikep ; whether face is struck through + css-name ; CSS name of face + ) + +(defun htmlize-face-set-from-keyword-attr (fstruct attr value) + ;; For ATTR and VALUE, set the equivalent value in FSTRUCT. + (cl-case attr + (:foreground + (setf (htmlize-fstruct-foreground fstruct) (htmlize-color-to-rgb value))) + (:background + (setf (htmlize-fstruct-background fstruct) (htmlize-color-to-rgb value))) + (:height + (setf (htmlize-fstruct-size fstruct) value)) + (:weight + (when (string-match (symbol-name value) "bold") + (setf (htmlize-fstruct-boldp fstruct) t))) + (:slant + (setf (htmlize-fstruct-italicp fstruct) (or (eq value 'italic) + (eq value 'oblique)))) + (:bold + (setf (htmlize-fstruct-boldp fstruct) value)) + (:italic + (setf (htmlize-fstruct-italicp fstruct) value)) + (:underline + (setf (htmlize-fstruct-underlinep fstruct) value)) + (:overline + (setf (htmlize-fstruct-overlinep fstruct) value)) + (:strike-through + (setf (htmlize-fstruct-strikep fstruct) value)))) + +(defun htmlize-face-size (face) + ;; The size (height) of FACE, taking inheritance into account. + ;; Only works in Emacs 21 and later. + (let* ((face-list (list face)) + (head face-list) + (tail face-list)) + (while head + (let ((inherit (face-attribute (car head) :inherit))) + (cond ((listp inherit) + (setcdr tail (cl-copy-list inherit)) + (setq tail (last tail))) + ((eq inherit 'unspecified)) + (t + (setcdr tail (list inherit)) + (setq tail (cdr tail))))) + (pop head)) + (let ((size-list + (cl-loop + for f in face-list + for h = (and (facep f) (face-attribute f :height)) + collect (if (eq h 'unspecified) nil h)))) + (cl-reduce 'htmlize-merge-size (cons nil size-list))))) + +(defun htmlize-face-css-name (face) + ;; Generate the css-name property for the given face. Emacs places + ;; no restrictions on the names of symbols that represent faces -- + ;; any characters may be in the name, even control chars. We try + ;; hard to beat the face name into shape, both esthetically and + ;; according to CSS1 specs. + (let ((name (downcase (symbol-name face)))) + (when (string-match "\\`font-lock-" name) + ;; font-lock-FOO-face -> FOO. + (setq name (replace-match "" t t name))) + (when (string-match "-face\\'" name) + ;; Drop the redundant "-face" suffix. + (setq name (replace-match "" t t name))) + (while (string-match "[^-a-zA-Z0-9]" name) + ;; Drop the non-alphanumerics. + (setq name (replace-match "X" t t name))) + (when (string-match "\\`[-0-9]" name) + ;; CSS identifiers may not start with a digit. + (setq name (concat "X" name))) + ;; After these transformations, the face could come out empty. + (when (equal name "") + (setq name "face")) + ;; Apply the prefix. + (concat htmlize-css-name-prefix name))) + +(defun htmlize-face-to-fstruct-1 (face) + "Convert Emacs face FACE to fstruct, internal." + (let ((fstruct (make-htmlize-fstruct + :foreground (htmlize-color-to-rgb + (htmlize-face-foreground face)) + :background (htmlize-color-to-rgb + (htmlize-face-background face))))) + ;; GNU Emacs + (dolist (attr '(:weight :slant :underline :overline :strike-through)) + (let ((value (face-attribute face attr nil t))) + (when (and value (not (eq value 'unspecified))) + (htmlize-face-set-from-keyword-attr fstruct attr value)))) + (let ((size (htmlize-face-size face))) + (unless (eql size 1.0) ; ignore non-spec + (setf (htmlize-fstruct-size fstruct) size))) + (setf (htmlize-fstruct-css-name fstruct) (htmlize-face-css-name face)) + fstruct)) + +(defun htmlize-face-to-fstruct (face) + (let* ((face-list (or (and (symbolp face) + (cdr (assq face face-remapping-alist))) + (list face))) + (fstruct (htmlize-merge-faces + (mapcar (lambda (face) + (if (symbolp face) + (or (htmlize-get-override-fstruct face) + (htmlize-face-to-fstruct-1 face)) + (htmlize-attrlist-to-fstruct face))) + (nreverse face-list))))) + (when (symbolp face) + (setf (htmlize-fstruct-css-name fstruct) (htmlize-face-css-name face))) + fstruct)) + +(defmacro htmlize-copy-attr-if-set (attr-list dest source) + ;; Generate code with the following pattern: + ;; (progn + ;; (when (htmlize-fstruct-ATTR source) + ;; (setf (htmlize-fstruct-ATTR dest) (htmlize-fstruct-ATTR source))) + ;; ...) + ;; for the given list of boolean attributes. + (cons 'progn + (cl-loop for attr in attr-list + for attr-sym = (intern (format "htmlize-fstruct-%s" attr)) + collect `(when (,attr-sym ,source) + (setf (,attr-sym ,dest) (,attr-sym ,source)))))) + +(defun htmlize-merge-size (merged next) + ;; Calculate the size of the merge of MERGED and NEXT. + (cond ((null merged) next) + ((integerp next) next) + ((null next) merged) + ((floatp merged) (* merged next)) + ((integerp merged) (round (* merged next))))) + +(defun htmlize-merge-two-faces (merged next) + (htmlize-copy-attr-if-set + (foreground background boldp italicp underlinep overlinep strikep) + merged next) + (setf (htmlize-fstruct-size merged) + (htmlize-merge-size (htmlize-fstruct-size merged) + (htmlize-fstruct-size next))) + merged) + +(defun htmlize-merge-faces (fstruct-list) + (cond ((null fstruct-list) + ;; Nothing to do, return a dummy face. + (make-htmlize-fstruct)) + ((null (cdr fstruct-list)) + ;; Optimize for the common case of a single face, simply + ;; return it. + (car fstruct-list)) + (t + (cl-reduce #'htmlize-merge-two-faces + (cons (make-htmlize-fstruct) fstruct-list))))) + +;; GNU Emacs 20+ supports attribute lists in `face' properties. For +;; example, you can use `(:foreground "red" :weight bold)' as an +;; overlay's "face", or you can even use a list of such lists, etc. +;; We call those "attrlists". +;; +;; htmlize supports attrlist by converting them to fstructs, the same +;; as with regular faces. + +(defun htmlize-attrlist-to-fstruct (attrlist &optional name) + ;; Like htmlize-face-to-fstruct, but accepts an ATTRLIST as input. + (let ((fstruct (make-htmlize-fstruct))) + (cond ((eq (car attrlist) 'foreground-color) + ;; ATTRLIST is (foreground-color . COLOR) + (setf (htmlize-fstruct-foreground fstruct) + (htmlize-color-to-rgb (cdr attrlist)))) + ((eq (car attrlist) 'background-color) + ;; ATTRLIST is (background-color . COLOR) + (setf (htmlize-fstruct-background fstruct) + (htmlize-color-to-rgb (cdr attrlist)))) + (t + ;; ATTRLIST is a plist. + (while attrlist + (let ((attr (pop attrlist)) + (value (pop attrlist))) + (when (and value (not (eq value 'unspecified))) + (htmlize-face-set-from-keyword-attr fstruct attr value)))))) + (setf (htmlize-fstruct-css-name fstruct) (or name "custom")) + fstruct)) + +(defun htmlize-decode-face-prop (prop) + "Turn face property PROP into a list of face-like objects." + ;; PROP can be a symbol naming a face, a string naming such a + ;; symbol, a cons (foreground-color . COLOR) or (background-color + ;; COLOR), a property list (:attr1 val1 :attr2 val2 ...), or a list + ;; of any of those. + ;; + ;; (htmlize-decode-face-prop 'face) -> (face) + ;; (htmlize-decode-face-prop '(face1 face2)) -> (face1 face2) + ;; (htmlize-decode-face-prop '(:attr "val")) -> ((:attr "val")) + ;; (htmlize-decode-face-prop '((:attr "val") face (foreground-color "red"))) + ;; -> ((:attr "val") face (foreground-color "red")) + ;; + ;; Unrecognized atoms or non-face symbols/strings are silently + ;; stripped away. + (cond ((null prop) + nil) + ((symbolp prop) + (and (facep prop) + (list prop))) + ((stringp prop) + (and (facep (intern-soft prop)) + (list prop))) + ((atom prop) + nil) + ((and (symbolp (car prop)) + (eq ?: (aref (symbol-name (car prop)) 0))) + (list prop)) + ((or (eq (car prop) 'foreground-color) + (eq (car prop) 'background-color)) + (list prop)) + (t + (apply #'nconc (mapcar #'htmlize-decode-face-prop prop))))) + +(defun htmlize-get-override-fstruct (face) + (let* ((raw-def (plist-get htmlize-face-overrides face)) + (def (cond ((stringp raw-def) (list :foreground raw-def)) + ((listp raw-def) raw-def) + (t + (error (format (concat "face override must be an " + "attribute list or string, got %s") + raw-def)))))) + (and def + (htmlize-attrlist-to-fstruct def (symbol-name face))))) + +(defun htmlize-make-face-map (faces) + ;; Return a hash table mapping Emacs faces to htmlize's fstructs. + ;; The keys are either face symbols or attrlists, so the test + ;; function must be `equal'. + (let ((face-map (make-hash-table :test 'equal)) + css-names) + (dolist (face faces) + (unless (gethash face face-map) + ;; Haven't seen FACE yet; convert it to an fstruct and cache + ;; it. + (let ((fstruct (htmlize-face-to-fstruct face))) + (setf (gethash face face-map) fstruct) + (let* ((css-name (htmlize-fstruct-css-name fstruct)) + (new-name css-name) + (i 0)) + ;; Uniquify the face's css-name by using NAME-1, NAME-2, + ;; etc. + (while (member new-name css-names) + (setq new-name (format "%s-%s" css-name (cl-incf i)))) + (unless (equal new-name css-name) + (setf (htmlize-fstruct-css-name fstruct) new-name)) + (push new-name css-names))))) + face-map)) + +(defun htmlize-unstringify-face (face) + "If FACE is a string, return it interned, otherwise return it unchanged." + (if (stringp face) + (intern face) + face)) + +(defun htmlize-faces-in-buffer () + "Return a list of faces used in the current buffer. +This is the set of faces specified by the `face' text property and by buffer +overlays that specify `face'." + (let (faces) + ;; Faces used by text properties. + (let ((pos (point-min)) face-prop next) + (while (< pos (point-max)) + (setq face-prop (get-text-property pos 'face) + next (or (next-single-property-change pos 'face) (point-max))) + (setq faces (cl-nunion (htmlize-decode-face-prop face-prop) + faces :test 'equal)) + (setq pos next))) + ;; Faces used by overlays. + (dolist (overlay (overlays-in (point-min) (point-max))) + (let ((face-prop (overlay-get overlay 'face))) + (setq faces (cl-nunion (htmlize-decode-face-prop face-prop) + faces :test 'equal)))) + faces)) + +;; htmlize-faces-at-point returns the faces in use at point. The +;; faces are sorted by increasing priority, i.e. the last face takes +;; precedence. +;; +;; This returns all the faces in the `face' property and all the faces +;; in the overlays at point. + +(defun htmlize-faces-at-point () + (let (all-faces) + ;; Faces from text properties. + (let ((face-prop (get-text-property (point) 'face))) + ;; we need to reverse the `face' prop because we want + ;; more specific faces to come later + (setq all-faces (nreverse (htmlize-decode-face-prop face-prop)))) + ;; Faces from overlays. + (let ((overlays + ;; Collect overlays at point that specify `face'. + (cl-delete-if-not (lambda (o) + (overlay-get o 'face)) + (nreverse (overlays-at (point) t)))) + list face-prop) + (dolist (overlay overlays) + (setq face-prop (overlay-get overlay 'face) + list (nconc (htmlize-decode-face-prop face-prop) list))) + ;; Under "Merging Faces" the manual explicitly states + ;; that faces specified by overlays take precedence over + ;; faces specified by text properties. + (setq all-faces (nconc all-faces list))) + all-faces)) + +;; htmlize supports generating HTML in several flavors, some of which +;; use CSS, and others the element. We take an OO approach and +;; define "methods" that indirect to the functions that depend on +;; `htmlize-output-type'. The currently used methods are `doctype', +;; `insert-head', `body-tag', `pre-tag', and `text-markup'. Not all +;; output types define all methods. +;; +;; Methods are called either with (htmlize-method METHOD ARGS...) +;; special form, or by accessing the function with +;; (htmlize-method-function 'METHOD) and calling (funcall FUNCTION). +;; The latter form is useful in tight loops because `htmlize-method' +;; conses. + +(defmacro htmlize-method (method &rest args) + ;; Expand to (htmlize-TYPE-METHOD ...ARGS...). TYPE is the value of + ;; `htmlize-output-type' at run time. + `(funcall (htmlize-method-function ',method) ,@args)) + +(defun htmlize-method-function (method) + ;; Return METHOD's function definition for the current output type. + ;; The returned object can be safely funcalled. + (let ((sym (intern (format "htmlize-%s-%s" htmlize-output-type method)))) + (indirect-function (if (fboundp sym) + sym + (let ((default (intern (concat "htmlize-default-" + (symbol-name method))))) + (if (fboundp default) + default + 'ignore)))))) + +(defvar htmlize-memoization-table (make-hash-table :test 'equal)) + +(defmacro htmlize-memoize (key generator) + "Return the value of GENERATOR, memoized as KEY. +That means that GENERATOR will be evaluated and returned the first time +it's called with the same value of KEY. All other times, the cached +\(memoized) value will be returned." + (let ((value (cl-gensym))) + `(let ((,value (gethash ,key htmlize-memoization-table))) + (unless ,value + (setq ,value ,generator) + (setf (gethash ,key htmlize-memoization-table) ,value)) + ,value))) + +;;; Default methods. + +(defun htmlize-default-doctype () + nil ; no doc-string + ;; Note that the `font' output is technically invalid under this DTD + ;; because the DTD doesn't allow embedding in
.
+  ""
+  )
+
+(defun htmlize-default-body-tag (face-map)
+  nil					; no doc-string
+  face-map ; shut up the byte-compiler
+  "")
+
+(defun htmlize-default-pre-tag (face-map)
+  nil					; no doc-string
+  face-map ; shut up the byte-compiler
+  "
")
+
+
+;;; CSS based output support.
+
+;; Internal function; not a method.
+(defun htmlize-css-specs (fstruct)
+  (let (result)
+    (when (htmlize-fstruct-foreground fstruct)
+      (push (format "color: %s;" (htmlize-fstruct-foreground fstruct))
+	    result))
+    (when (htmlize-fstruct-background fstruct)
+      (push (format "background-color: %s;"
+		    (htmlize-fstruct-background fstruct))
+	    result))
+    (let ((size (htmlize-fstruct-size fstruct)))
+      (when (and size (not (eq htmlize-ignore-face-size t)))
+	(cond ((floatp size)
+	       (push (format "font-size: %d%%;" (* 100 size)) result))
+	      ((not (eq htmlize-ignore-face-size 'absolute))
+	       (push (format "font-size: %spt;" (/ size 10.0)) result)))))
+    (when (htmlize-fstruct-boldp fstruct)
+      (push "font-weight: bold;" result))
+    (when (htmlize-fstruct-italicp fstruct)
+      (push "font-style: italic;" result))
+    (when (htmlize-fstruct-underlinep fstruct)
+      (push "text-decoration: underline;" result))
+    (when (htmlize-fstruct-overlinep fstruct)
+      (push "text-decoration: overline;" result))
+    (when (htmlize-fstruct-strikep fstruct)
+      (push "text-decoration: line-through;" result))
+    (nreverse result)))
+
+(defun htmlize-css-insert-head (buffer-faces face-map)
+  (insert "    \n"))
+
+(defun htmlize-css-text-markup (fstruct-list buffer)
+  ;; Open the markup needed to insert text colored with FACES into
+  ;; BUFFER.  Return the function that closes the markup.
+
+  ;; In CSS mode, this is easy: just nest the text in one  tag for each face in FSTRUCT-LIST.
+  (dolist (fstruct fstruct-list)
+    (princ "" buffer))
+  (htmlize-lexlet ((fstruct-list fstruct-list) (buffer buffer))
+    (lambda ()
+      (dolist (fstruct fstruct-list)
+        (ignore fstruct)                ; shut up the byte-compiler
+        (princ "" buffer)))))
+
+;; `inline-css' output support.
+
+(defun htmlize-inline-css-body-tag (face-map)
+  (format ""
+	  (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map))
+		     " ")))
+
+(defun htmlize-inline-css-pre-tag (face-map)
+  (if htmlize-pre-style
+      (format "
"
+              (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map))
+                         " "))
+    (format "
")))
+
+(defun htmlize-inline-css-text-markup (fstruct-list buffer)
+  (let* ((merged (htmlize-merge-faces fstruct-list))
+	 (style (htmlize-memoize
+		 merged
+		 (let ((specs (htmlize-css-specs merged)))
+		   (and specs
+			(mapconcat #'identity (htmlize-css-specs merged) " "))))))
+    (when style
+      (princ "" buffer))
+    (htmlize-lexlet ((style style) (buffer buffer))
+      (lambda ()
+        (when style
+          (princ "" buffer))))))
+
+;;; `font' tag based output support.
+
+(defun htmlize-font-body-tag (face-map)
+  (let ((fstruct (gethash 'default face-map)))
+    (format ""
+	    (htmlize-fstruct-foreground fstruct)
+	    (htmlize-fstruct-background fstruct))))
+
+(defun htmlize-font-pre-tag (face-map)
+  (if htmlize-pre-style
+      (let ((fstruct (gethash 'default face-map)))
+        (format "
"
+                (htmlize-fstruct-foreground fstruct)
+                (htmlize-fstruct-background fstruct)))
+    (format "
")))
+       
+(defun htmlize-font-text-markup (fstruct-list buffer)
+  ;; In `font' mode, we use the traditional HTML means of altering
+  ;; presentation:  tag for colors,  for bold,  for
+  ;; underline, and  for strike-through.
+  (let* ((merged (htmlize-merge-faces fstruct-list))
+	 (markup (htmlize-memoize
+		  merged
+		  (cons (concat
+			 (and (htmlize-fstruct-foreground merged)
+			      (format "" (htmlize-fstruct-foreground merged)))
+			 (and (htmlize-fstruct-boldp merged)      "")
+			 (and (htmlize-fstruct-italicp merged)    "")
+			 (and (htmlize-fstruct-underlinep merged) "")
+			 (and (htmlize-fstruct-strikep merged)    ""))
+			(concat
+			 (and (htmlize-fstruct-strikep merged)    "")
+			 (and (htmlize-fstruct-underlinep merged) "")
+			 (and (htmlize-fstruct-italicp merged)    "")
+			 (and (htmlize-fstruct-boldp merged)      "")
+			 (and (htmlize-fstruct-foreground merged) ""))))))
+    (princ (car markup) buffer)
+    (htmlize-lexlet ((markup markup) (buffer buffer))
+      (lambda ()
+        (princ (cdr markup) buffer)))))
+
+(defun htmlize-buffer-1 ()
+  ;; Internal function; don't call it from outside this file.  Htmlize
+  ;; current buffer, writing the resulting HTML to a new buffer, and
+  ;; return it.  Unlike htmlize-buffer, this doesn't change current
+  ;; buffer or use switch-to-buffer.
+  (save-excursion
+    ;; Protect against the hook changing the current buffer.
+    (save-excursion
+      (run-hooks 'htmlize-before-hook))
+    ;; Convince font-lock support modes to fontify the entire buffer
+    ;; in advance.
+    (htmlize-ensure-fontified)
+    (clrhash htmlize-extended-character-cache)
+    (clrhash htmlize-memoization-table)
+    ;; It's important that the new buffer inherits default-directory
+    ;; from the current buffer.
+    (let ((htmlbuf (generate-new-buffer (if (buffer-file-name)
+                                            (htmlize-make-file-name
+                                             (file-name-nondirectory
+                                              (buffer-file-name)))
+                                          "*html*")))
+          (completed nil))
+      (unwind-protect
+          (let* ((buffer-faces (htmlize-faces-in-buffer))
+                 (face-map (htmlize-make-face-map (cl-adjoin 'default buffer-faces)))
+                 (places (cl-gensym))
+                 (title (if (buffer-file-name)
+                            (file-name-nondirectory (buffer-file-name))
+                          (buffer-name))))
+            (when htmlize-generate-hyperlinks
+              (htmlize-create-auto-links))
+            (when htmlize-replace-form-feeds
+              (htmlize-shadow-form-feeds))
+
+            ;; Initialize HTMLBUF and insert the HTML prolog.
+            (with-current-buffer htmlbuf
+              (buffer-disable-undo)
+              (insert (htmlize-method doctype) ?\n
+                      (format "\n"
+                              htmlize-version htmlize-output-type)
+                      "\n  ")
+              (put places 'head-start (point-marker))
+              (insert "\n"
+                      "    " (htmlize-protect-string title) "\n"
+                      (if htmlize-html-charset
+                          (format (concat "    \n")
+                                  htmlize-html-charset)
+                        "")
+                      htmlize-head-tags)
+              (htmlize-method insert-head buffer-faces face-map)
+              (insert "  ")
+              (put places 'head-end (point-marker))
+              (insert "\n  ")
+              (put places 'body-start (point-marker))
+              (insert (htmlize-method body-tag face-map)
+                      "\n    ")
+              (put places 'content-start (point-marker))
+              (insert (htmlize-method pre-tag face-map) "\n"))
+            (let ((text-markup
+                   ;; Get the inserter method, so we can funcall it inside
+                   ;; the loop.  Not calling `htmlize-method' in the loop
+                   ;; body yields a measurable speed increase.
+                   (htmlize-method-function 'text-markup))
+                  ;; Declare variables used in loop body outside the loop
+                  ;; because it's faster to establish `let' bindings only
+                  ;; once.
+                  next-change text face-list trailing-ellipsis
+                  fstruct-list last-fstruct-list
+                  (close-markup (lambda ())))
+              ;; This loop traverses and reads the source buffer, appending
+              ;; the resulting HTML to HTMLBUF.  This method is fast
+              ;; because: 1) it doesn't require examining the text
+              ;; properties char by char (htmlize-next-face-change is used
+              ;; to move between runs with the same face), and 2) it doesn't
+              ;; require frequent buffer switches, which are slow because
+              ;; they rebind all buffer-local vars.
+              (goto-char (point-min))
+              (while (not (eobp))
+                (setq next-change (htmlize-next-face-change (point)))
+                ;; Get faces in use between (point) and NEXT-CHANGE, and
+                ;; convert them to fstructs.
+                (setq face-list (htmlize-faces-at-point)
+                      fstruct-list (delq nil (mapcar (lambda (f)
+                                                       (gethash f face-map))
+                                                     face-list)))
+                (cl-multiple-value-setq (text trailing-ellipsis)
+                  (htmlize-extract-text (point) next-change trailing-ellipsis))
+                ;; Don't bother writing anything if there's no text (this
+                ;; happens in invisible regions).
+                (when (> (length text) 0)
+                  ;; Open the new markup if necessary and insert the text.
+                  (when (not (cl-equalp fstruct-list last-fstruct-list))
+                    (funcall close-markup)
+                    (setq last-fstruct-list fstruct-list
+                          close-markup (funcall text-markup fstruct-list htmlbuf)))
+                  (princ text htmlbuf))
+                (goto-char next-change))
+
+              ;; We've gone through the buffer; close the markup from
+              ;; the last run, if any.
+              (funcall close-markup))
+
+            ;; Insert the epilog and post-process the buffer.
+            (with-current-buffer htmlbuf
+              (insert "
") + (put places 'content-end (point-marker)) + (insert "\n ") + (put places 'body-end (point-marker)) + (insert "\n\n") + (htmlize-defang-local-variables) + (goto-char (point-min)) + (when htmlize-html-major-mode + ;; What sucks about this is that the minor modes, most notably + ;; font-lock-mode, won't be initialized. Oh well. + (funcall htmlize-html-major-mode)) + (set (make-local-variable 'htmlize-buffer-places) + (symbol-plist places)) + (run-hooks 'htmlize-after-hook) + (buffer-enable-undo)) + (setq completed t) + htmlbuf) + + (when (not completed) + (kill-buffer htmlbuf)) + (htmlize-delete-tmp-overlays))))) + +;; Utility functions. + +(defmacro htmlize-with-fontify-message (&rest body) + ;; When forcing fontification of large buffers in + ;; htmlize-ensure-fontified, inform the user that he is waiting for + ;; font-lock, not for htmlize to finish. + `(progn + (if (> (buffer-size) 65536) + (message "Forcing fontification of %s..." + (buffer-name (current-buffer)))) + ,@body + (if (> (buffer-size) 65536) + (message "Forcing fontification of %s...done" + (buffer-name (current-buffer)))))) + +(defun htmlize-ensure-fontified () + ;; If font-lock is being used, ensure that the "support" modes + ;; actually fontify the buffer. If font-lock is not in use, we + ;; don't care because, except in htmlize-file, we don't force + ;; font-lock on the user. + (when font-lock-mode + ;; In part taken from ps-print-ensure-fontified in GNU Emacs 21. + (when (and (boundp 'jit-lock-mode) + (symbol-value 'jit-lock-mode)) + (htmlize-with-fontify-message + (jit-lock-fontify-now (point-min) (point-max)))) + + (if (fboundp 'font-lock-ensure) + (font-lock-ensure) + ;; Emacs prior to 25.1 + (with-no-warnings + (font-lock-mode 1) + (font-lock-fontify-buffer))))) + + +;;;###autoload +(defun htmlize-buffer (&optional buffer) + "Convert BUFFER to HTML, preserving colors and decorations. + +The generated HTML is available in a new buffer, which is returned. +When invoked interactively, the new buffer is selected in the current +window. The title of the generated document will be set to the buffer's +file name or, if that's not available, to the buffer's name. + +Note that htmlize doesn't fontify your buffers, it only uses the +decorations that are already present. If you don't set up font-lock or +something else to fontify your buffers, the resulting HTML will be +plain. Likewise, if you don't like the choice of colors, fix the mode +that created them, or simply alter the faces it uses." + (interactive) + (let ((htmlbuf (with-current-buffer (or buffer (current-buffer)) + (htmlize-buffer-1)))) + (when (interactive-p) + (switch-to-buffer htmlbuf)) + htmlbuf)) + +;;;###autoload +(defun htmlize-region (beg end) + "Convert the region to HTML, preserving colors and decorations. +See `htmlize-buffer' for details." + (interactive "r") + ;; Don't let zmacs region highlighting end up in HTML. + (when (fboundp 'zmacs-deactivate-region) + (zmacs-deactivate-region)) + (let ((htmlbuf (save-restriction + (narrow-to-region beg end) + (htmlize-buffer-1)))) + (when (interactive-p) + (switch-to-buffer htmlbuf)) + htmlbuf)) + +(defun htmlize-region-for-paste (beg end) + "Htmlize the region and return just the HTML as a string. +This forces the `inline-css' style and only returns the HTML body, +but without the BODY tag. This should make it useful for inserting +the text to another HTML buffer." + (let* ((htmlize-output-type 'inline-css) + (htmlbuf (htmlize-region beg end))) + (unwind-protect + (with-current-buffer htmlbuf + (buffer-substring (plist-get htmlize-buffer-places 'content-start) + (plist-get htmlize-buffer-places 'content-end))) + (kill-buffer htmlbuf)))) + +(defun htmlize-region-save-screenshot (beg end) + "Save the htmlized (see `htmlize-region-for-paste') region in +the kill ring. Uses `inline-css', with style information in +`
' tags, so that the rendering of the marked up text
+approximates the buffer as closely as possible."
+  (interactive "r")
+  (let ((htmlize-pre-style t))
+    (kill-new (htmlize-region-for-paste beg end)))
+  (deactivate-mark))
+
+(defun htmlize-make-file-name (file)
+  "Make an HTML file name from FILE.
+
+In its default implementation, this simply appends `.html' to FILE.
+This function is called by htmlize to create the buffer file name, and
+by `htmlize-file' to create the target file name.
+
+More elaborate transformations are conceivable, such as changing FILE's
+extension to `.html' (\"file.c\" -> \"file.html\").  If you want them,
+overload this function to do it and htmlize will comply."
+  (concat file ".html"))
+
+;; Older implementation of htmlize-make-file-name that changes FILE's
+;; extension to ".html".
+;(defun htmlize-make-file-name (file)
+;  (let ((extension (file-name-extension file))
+;	(sans-extension (file-name-sans-extension file)))
+;    (if (or (equal extension "html")
+;	    (equal extension "htm")
+;	    (equal sans-extension ""))
+;	(concat file ".html")
+;      (concat sans-extension ".html"))))
+
+;;;###autoload
+(defun htmlize-file (file &optional target)
+  "Load FILE, fontify it, convert it to HTML, and save the result.
+
+Contents of FILE are inserted into a temporary buffer, whose major mode
+is set with `normal-mode' as appropriate for the file type.  The buffer
+is subsequently fontified with `font-lock' and converted to HTML.  Note
+that, unlike `htmlize-buffer', this function explicitly turns on
+font-lock.  If a form of highlighting other than font-lock is desired,
+please use `htmlize-buffer' directly on buffers so highlighted.
+
+Buffers currently visiting FILE are unaffected by this function.  The
+function does not change current buffer or move the point.
+
+If TARGET is specified and names a directory, the resulting file will be
+saved there instead of to FILE's directory.  If TARGET is specified and
+does not name a directory, it will be used as output file name."
+  (interactive (list (read-file-name
+		      "HTML-ize file: "
+		      nil nil nil (and (buffer-file-name)
+				       (file-name-nondirectory
+					(buffer-file-name))))))
+  (let ((output-file (if (and target (not (file-directory-p target)))
+			 target
+		       (expand-file-name
+			(htmlize-make-file-name (file-name-nondirectory file))
+			(or target (file-name-directory file)))))
+	;; Try to prevent `find-file-noselect' from triggering
+	;; font-lock because we'll fontify explicitly below.
+	(font-lock-mode nil)
+	(font-lock-auto-fontify nil)
+	(global-font-lock-mode nil)
+	;; Ignore the size limit for the purposes of htmlization.
+	(font-lock-maximum-size nil))
+    (with-temp-buffer
+      ;; Insert FILE into the temporary buffer.
+      (insert-file-contents file)
+      ;; Set the file name so normal-mode and htmlize-buffer-1 pick it
+      ;; up.  Restore it afterwards so with-temp-buffer's kill-buffer
+      ;; doesn't complain about killing a modified buffer.
+      (let ((buffer-file-name file))
+	;; Set the major mode for the sake of font-lock.
+	(normal-mode)
+	;; htmlize the buffer and save the HTML.
+	(with-current-buffer (htmlize-buffer-1)
+	  (unwind-protect
+	      (progn
+		(run-hooks 'htmlize-file-hook)
+		(write-region (point-min) (point-max) output-file))
+	    (kill-buffer (current-buffer)))))))
+  ;; I haven't decided on a useful return value yet, so just return
+  ;; nil.
+  nil)
+
+;;;###autoload
+(defun htmlize-many-files (files &optional target-directory)
+  "Convert FILES to HTML and save the corresponding HTML versions.
+
+FILES should be a list of file names to convert.  This function calls
+`htmlize-file' on each file; see that function for details.  When
+invoked interactively, you are prompted for a list of files to convert,
+terminated with RET.
+
+If TARGET-DIRECTORY is specified, the HTML files will be saved to that
+directory.  Normally, each HTML file is saved to the directory of the
+corresponding source file."
+  (interactive
+   (list
+    (let (list file)
+      ;; Use empty string as DEFAULT because setting DEFAULT to nil
+      ;; defaults to the directory name, which is not what we want.
+      (while (not (equal (setq file (read-file-name
+				     "HTML-ize file (RET to finish): "
+				     (and list (file-name-directory
+						(car list)))
+				     "" t))
+			 ""))
+	(push file list))
+      (nreverse list))))
+  ;; Verify that TARGET-DIRECTORY is indeed a directory.  If it's a
+  ;; file, htmlize-file will use it as target, and that doesn't make
+  ;; sense.
+  (and target-directory
+       (not (file-directory-p target-directory))
+       (error "target-directory must name a directory: %s" target-directory))
+  (dolist (file files)
+    (htmlize-file file target-directory)))
+
+;;;###autoload
+(defun htmlize-many-files-dired (arg &optional target-directory)
+  "HTMLize dired-marked files."
+  (interactive "P")
+  (htmlize-many-files (dired-get-marked-files nil arg) target-directory))
+
+(provide 'htmlize)
+
+;; Local Variables:
+;; byte-compile-warnings: (not unresolved obsolete)
+;; End:
+
+;;; htmlize.el ends here
diff --git a/src/org.css b/src/org.css
new file mode 100644
index 0000000..35f32f3
--- /dev/null
+++ b/src/org.css
@@ -0,0 +1,177 @@
+@import url("https://fonts.googleapis.com/css2?family=Lora:wght@500&display=swap");
+@import url("https://fonts.googleapis.com/css2?family=Inconsolata&display=swap");
+
+body {
+    margin: 40px auto;
+    width: 700px;
+    max-width: 100%;
+    line-height: 1.6;
+    font-size: 16px;
+    background: #fbf1c7;
+    color: #3a1616;
+    padding: 0 10px;
+    font-size: 18px;
+    line-height: 28px;
+    font-family: "Lora";
+    font-weight: 500;
+    /* Smooth the font a little bit, it's a
+         bit too bold on retina screens */
+    -webkit-font-smoothing: antialiased;
+}
+
+/* 
+ * Make a nice input form with rounded corners and hover animations
+ */
+input {
+    padding: 10px 16px;
+    margin: 2px 0;
+    box-sizing: border-box;
+    border: 2px solid #dabebe;
+    border-radius: 6px;
+    background: #fffff4;
+    color: #3a1616;
+    font-size: 16px;
+    -webkit-transition: 0.5s;
+    transition: 0.5s;
+    outline: none;
+}
+input:focus {
+    border: 2px solid #3a1616;
+}
+
+/* 
+ * The button is very similar to the input
+ */ 
+.button {
+    background-color: #fffff4;
+    border: none;
+    color: black;
+    padding: 6px 14px;
+    text-align: center;
+    text-decoration: none;
+    display: inline-block;
+    font-size: 16px;
+    margin: 4px 2px;
+    transition-duration: 0.4s;
+    cursor: pointer;
+    border: 2px solid #3a1616;
+    border-radius: 6px;
+}
+.button:hover {
+    background-color: #3a1616;
+    color: white;
+}
+
+/* 
+ * Various classes for messages of different kinds 
+ */ 
+.isa_info,
+.isa_success,
+.isa_warning,
+.isa_error {
+    width: 90%;
+    margin: 10px 0px;
+    padding: 12px;
+}
+.isa_info {
+    color: #00529b;
+    background-color: #bde5f8;
+}
+.isa_success {
+    color: #4f8a10;
+    background-color: #dff2bf;
+}
+.isa_warning {
+    color: #9f6000;
+    background-color: #feefb3;
+}
+.isa_error {
+    color: #d8000c;
+    background-color: #ffd2d2;
+}
+
+h1,
+h2,
+h3 {
+    line-height: 1.2;
+    font-family: "Lora";
+}
+
+img {
+    width: 500px;
+    max-width: 100%;
+    border-radius: 10px;
+    text-align: center;
+}
+
+/* 
+ * Bold hover animations on links and clickables 
+ */
+a {
+    cursor: pointer;
+    /* color: #217ab7; */
+    color: #d16286;
+    line-height: inherit;
+    transition: .14s;
+}
+a:hover {
+    color: white;
+    background-color: #3297d3;
+}
+a:visited {
+    /* color: #43458b; */
+    color: #d16286;
+    border-color: #43458b;
+}
+a:visited:hover {
+    color: white;
+    background-color: #9251ac;
+}
+
+/*
+ * Moving sizes of headings up
+ */
+h2 {
+    font-size: 2em;
+}
+
+h3 {
+    font-size: 1.5em;
+}
+
+h4 {
+    font-size: 1.17em;
+}
+
+h5 {
+    font-size: 1em;
+}
+
+pre {
+    font-family: "Inconsolata", monospace;
+}
+
+/*
+ * Fancy horizontal rule
+ */
+hr {
+  border: none;
+  border-top: 3px double #333;
+  color: #333;
+  overflow: visible;
+  text-align: center;
+  height: 5px;
+}
+
+hr:after {
+  background: #fbf1c7;
+  content: 'ยง';
+  padding: 0 4px;
+  position: relative;
+  top: -13px;
+}
+
+::selection {
+    color: white;
+    background: #ff4081;
+}