How is the target section of a block header calculated? I've been poking around https://en.bitcoin.it/wiki/Target and I've found the current target, but I can't seem to find how it's generated. It's supposedly generated when the difficulty is adjusted, as stated here: https://en.bitcoin.it/wiki/Block_hashing_algorithm, but what exactly is the algorithm for generating the target? If someone could show me some psuedocode that would be great.
The target section of the block header is called nBits in the code.  nBits is a 32-bit compact encoding of a 256-bit target threshold.  It works like scientific notation, except that it uses base-256 instead of base-10.  For example, if nBits is equal to 0x181b8330, you would calculate it like this:

Or, more simply, you'd use the same shorthand you use with regular scientific notation:

At a re-target point (every 2,016th block), Bitcoin Core adjusts nBits according to the rules described in this answer except that it is important to note that when difficulty changes by p percent, nBits is adjusted by the inverse (-p percent).  That's because a lower target is harder to reach the way Bitcoin is implemented.
It's also important to note that you can't just adjust the exponent part of nBits in the obvious way because when Satoshi first wrote the code, he inherited from a signed type---so extra care must be taken not to create a negative nBits value.  The Bitcoin.org Developer Reference has more details (but be careful, I haven't yet had an expert review that section).